最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

最近較流行的效果 Android自定義View實現(xiàn)傾斜列表/圖片

 更新時間:2016年06月07日 12:38:42   作者:pengkv  
最近較流行的效果,這篇文章主要介紹了Android自定義View實現(xiàn)傾斜列表/圖片的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下

先看看效果圖:

實現(xiàn)思路:擦除圖片相應(yīng)的角,然后層疊圖片,產(chǎn)生傾斜效果

代碼實現(xiàn):

1、定義屬性

在values文件夾下的attrs文件添加以下代碼

<resources>
  <declare-styleable name="TiltView">
    <attr name="type" format="integer" />
  </declare-styleable>
</resources>

2、自定義布局

public class TiltView extends ImageView {

  private int imageWidth;//圖片寬度
  private int imageHeight;//圖片高度
  private double angle = 10 * Math.PI / 180;//三角形角度
  private int triangleHeight;//三角形高度
  private Paint paint;//畫筆
  private Path path;//繪制路徑
  private int type;//傾斜圖片的類型

  public TiltView(Context context) {
    this(context, null);
  }

  public TiltView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public TiltView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.TiltView);
    type = array.getInteger(R.styleable.TiltView_type, 1);
    array.recycle();
  }


  //重測大小
  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    imageWidth = measureSpec(widthMeasureSpec);
    imageHeight = measureSpec(heightMeasureSpec);
    setMeasuredDimension(imageWidth, imageHeight); //設(shè)置View的大小
    triangleHeight = (int) (Math.abs(Math.tan(angle) * imageHeight));
  }

  //測量長度
  private int measureSpec(int measureSpec) {
    int minLength = 200;
    int mode = MeasureSpec.getMode(measureSpec);
    int length = MeasureSpec.getSize(measureSpec);

    if (mode == MeasureSpec.AT_MOST) {
      length = Math.min(length, minLength);
    }
    return length;
  }

  @Override
  protected void onDraw(Canvas canvas) {
    initPaint();

    Bitmap mBitmap = Bitmap.createBitmap(imageWidth, imageHeight, Bitmap.Config.ARGB_8888); //初始化Bitmap
    Canvas mCanvas = new Canvas(mBitmap);//創(chuàng)建畫布,并繪制mBitmap
    Bitmap mBackBitmap = ((BitmapDrawable) getDrawable()).getBitmap();
    mCanvas.drawBitmap(resizeBitmap(mBackBitmap), 0, 0, null);//繪制Bitmap

    setTriangle();
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
    mCanvas.drawPath(path, paint);

    canvas.drawBitmap(mBitmap, 0, 0, null);
  }

  //初始化畫筆
  private void initPaint() {
    paint = new Paint();
    paint.setDither(true);//設(shè)定是否使用圖像抖動處理,會使繪制出來的圖片顏色更加平滑和飽滿,圖像更加清晰
    paint.setAntiAlias(true);//設(shè)置抗鋸齒
    paint.setStrokeWidth(5);
    paint.setStyle(Paint.Style.FILL);
    paint.setStrokeCap(Paint.Cap.ROUND);
    paint.setStrokeJoin(Paint.Join.ROUND);//圓角
  }


  //設(shè)置三角形區(qū)域
  private void setTriangle() {
    path = new Path();
    switch (type) {
      case 1://右下角
        path.moveTo(0, imageHeight);
        path.lineTo(imageWidth, imageHeight);
        path.lineTo(imageWidth, imageHeight - triangleHeight);
        path.lineTo(0, imageHeight);
        break;
      case 2://左上角+左下角
        path.moveTo(0, triangleHeight);
        path.lineTo(imageWidth, 0);
        path.lineTo(0, 0);
        path.lineTo(0, imageHeight);
        path.lineTo(imageWidth, imageHeight);
        path.lineTo(0, imageHeight - triangleHeight);
        break;
      case 3://右上角+右下角
        path.moveTo(imageWidth, triangleHeight);
        path.lineTo(0, 0);
        path.lineTo(imageWidth, 0);
        path.lineTo(imageWidth, imageHeight);
        path.lineTo(0, imageHeight);
        path.lineTo(imageWidth, imageHeight - triangleHeight);
        break;
      case 4://右上角
        path.moveTo(0, 0);
        path.lineTo(imageWidth, 0);
        path.lineTo(imageWidth, triangleHeight);
        path.lineTo(0, 0);
        break;
      case 5://左上角
        path.moveTo(0, 0);
        path.lineTo(imageWidth, 0);
        path.lineTo(0, triangleHeight);
        path.lineTo(0, 0);
        break;
    }
  }

  //重新調(diào)節(jié)圖片大小
  private Bitmap resizeBitmap(Bitmap bitmap) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    // 設(shè)置想要的大小
    int newWidth = imageWidth;
    int newHeight = imageHeight;
    // 計算縮放比例
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    // 取得想要縮放的matrix參數(shù)
    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);
    // 得到新的圖片
    return Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
  }

}

3、布局代碼調(diào)用

//其中android:layout_marginTop="-15dp"對效果實現(xiàn)有很大的作用
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">

  <com.pengkv.may.widget.TiltView
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:src="@drawable/sample_0"
    app:type="1" />

  <com.pengkv.may.widget.TiltView
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_marginTop="-15dp"
    android:src="@drawable/sample_1"
    app:type="2" />

  <com.pengkv.may.widget.TiltView
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_marginTop="-15dp"
    android:src="@drawable/sample_2"
    app:type="4" />

</LinearLayout>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 強制Android應(yīng)用使用某個Locale的方法

    強制Android應(yīng)用使用某個Locale的方法

    這篇文章主要介紹了強制Android應(yīng)用使用某個Locale的方法,涉及Android基于Locale進行語言設(shè)置的相關(guān)技巧,需要的朋友可以參考下
    2015-10-10
  • Android編程之控件可拖動的實現(xiàn)方法

    Android編程之控件可拖動的實現(xiàn)方法

    這篇文章主要介紹了Android編程之控件可拖動的實現(xiàn)方法,實例分析了Android響應(yīng)點擊及觸摸事件的相關(guān)技巧,需要的朋友可以參考下
    2016-02-02
  • Android滑動沖突的完美解決

    Android滑動沖突的完美解決

    這篇文章主要為大家詳細介紹了Android滑動沖突的完美解決方案,針對三種滑動沖突場景進行解決,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • Android觀察者模式實例分析

    Android觀察者模式實例分析

    這篇文章主要介紹了Android觀察者模式,實例分析了Android觀察者模式的原理與相關(guān)使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-04-04
  • 深入理解Android熱修復(fù)技術(shù)原理之代碼熱修復(fù)技術(shù)

    深入理解Android熱修復(fù)技術(shù)原理之代碼熱修復(fù)技術(shù)

    在各種 Android 熱修復(fù)方案中,Andfix的即時生效令人印象深刻,它稍顯另類, 并不需要重新啟動,而是在加載補丁后直接對方法進行替換就可以完成修復(fù),然而它的使用限制也遭遇到更多的質(zhì)疑
    2021-06-06
  • Android 如何實現(xiàn)亮度自動調(diào)節(jié)

    Android 如何實現(xiàn)亮度自動調(diào)節(jié)

    這篇文章主要介紹了Android 如何實現(xiàn)亮度自動調(diào)節(jié),幫助大家更好的理解和學(xué)習(xí)使用Android開發(fā),感興趣的朋友可以了解下
    2021-04-04
  • Android啟動頁出現(xiàn)白屏、黑屏的解決方案

    Android啟動頁出現(xiàn)白屏、黑屏的解決方案

    這篇文章主要給大家介紹了關(guān)于Android啟動頁出現(xiàn)白屏、黑屏的解決方案,這一個需求是每位Android開發(fā)者都需要的,最近發(fā)現(xiàn)了一個不錯的解決方法,所以分享給大家,文中給出了詳細的介紹,需要的朋友可以參考下。
    2017-12-12
  • Android仿qq頂部消息欄效果

    Android仿qq頂部消息欄效果

    這篇文章主要介紹了Android仿qq頂部消息欄效果,需要的朋友可以參考下
    2018-04-04
  • 使用VideoView播放App中的資源文件

    使用VideoView播放App中的資源文件

    這篇文章主要介紹了使用VideoView播放App中的資源文件,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-06-06
  • Android zygote啟動流程詳解

    Android zygote啟動流程詳解

    這篇文章主要介紹了Android zygote啟動流程的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用Android開發(fā),感興趣的朋友可以了解下
    2021-03-03

最新評論

金川县| 万盛区| 巨野县| 平定县| 诏安县| 平和县| 漳州市| 和田市| 久治县| 石河子市| 吕梁市| 镇原县| 新沂市| 棋牌| 绥化市| 南宁市| 宜丰县| 尼玛县| 平湖市| 贵德县| 襄汾县| 台山市| 闽清县| 新源县| 合水县| 临沭县| 宝清县| 玉树县| 张家口市| 遵义市| 五台县| 罗定市| 东安县| 北宁市| 锦州市| 罗田县| 东兰县| 长乐市| 舒兰市| 泗水县| 涡阳县|