日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

Android 自定义View Canvas —— Bitmap

發布時間:2023/11/27 生活经验 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android 自定义View Canvas —— Bitmap 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Bitmap 繪制圖片 常用的方法有一下幾種

(1)?drawBitmap(@NonNull Bitmap bitmap, float left, float top, @Nullable Paint paint)

(2)?drawBitmap(@NonNull Bitmap bitmap, @NonNull Matrix matrix, @Nullable Paint paint)

(3)?drawBitmap(@NonNull Bitmap bitmap, @Nullable Rect src, @NonNull RectF dst, @Nullable Paint paint)

?

1?drawBitmap(@NonNull Bitmap bitmap, float left, float top, @Nullable Paint paint)

?1.1 使用BitmapFactory解析圖片

    paint.setAntiAlias(true);// 設置畫筆的style (Paint.Style.FILL填充,Paint.Style.STROKE描邊,Paint.Style.FILL_AND_STROKE填充加描邊  )paint.setStyle(Paint.Style.FILL);// 設置畫筆的顏色 這里只是繪制圖片感覺用不到就注釋掉了
//        paint.setColor(Color.RED);
//        paint.setStrokeWidth(10f);Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.girl);canvas.drawBitmap(bitmap, 100, 100, paint);

或者

        paint.setAntiAlias(true);// 設置畫筆的style (Paint.Style.FILL填充,Paint.Style.STROKE描邊,Paint.Style.FILL_AND_STROKE填充加描邊  )paint.setStyle(Paint.Style.FILL);// 設置畫筆的顏色 這里只是繪制圖片感覺用不到就注釋掉了
//        paint.setColor(Color.RED);
//        paint.setStrokeWidth(10f);
//        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.girl);@SuppressLint("ResourceType")Bitmap bitmap =BitmapFactory.decodeStream(getResources().openRawResource(R.mipmap.girl));canvas.drawBitmap(bitmap, 100, 100, paint);

效果圖

1.2??使用BitmapDrawable解析圖片

  paint.setAntiAlias(true);// 設置畫筆的style (Paint.Style.FILL填充,Paint.Style.STROKE描邊,Paint.Style.FILL_AND_STROKE填充加描邊  )paint.setStyle(Paint.Style.FILL);// 設置畫筆的顏色 這里只是繪制圖片感覺用不到就注釋掉了// paint.setColor(Color.RED);// paint.setStrokeWidth(10f);// BitmapDrawableBitmapDrawable bitmapDrawable = (BitmapDrawable) getResources().getDrawable(R.mipmap.girl);// 得到BitmapBitmap bitmap = bitmapDrawable.getBitmap();canvas.drawBitmap(bitmap, 100, 100, paint);

效果圖如下:

1.3?使用InputStream和BitmapDrawable繪制

  paint.setAntiAlias(true);// 設置畫筆的style (Paint.Style.FILL填充,Paint.Style.STROKE描邊,Paint.Style.FILL_AND_STROKE填充加描邊  )paint.setStyle(Paint.Style.FILL);// 設置畫筆的顏色 這里只是繪制圖片感覺用不到就注釋掉了// paint.setColor(Color.RED);// paint.setStrokeWidth(10f);// InputStream得到資源流@SuppressLint("ResourceType")InputStream in = getResources().openRawResource(R.mipmap.girl);// BitmapDrawable 解析數據流BitmapDrawable bitmapDrawable =  new BitmapDrawable(in);// 得到圖片Bitmap bitmap = bitmapDrawable.getBitmap();// 得到Bitmapcanvas.drawBitmap(bitmap, 100, 100, paint);

效果圖如下:

2?drawBitmap(@NonNull Bitmap bitmap, @NonNull Matrix matrix, @Nullable Paint paint)

Matrix 是變形矩陣 這里先簡單的說下其一般的使用方法, new 一下然后使用其方法即可,后面在開一篇Matrix的文章

下面是圖片放大的方法如下:

paint.setAntiAlias(true);// 設置畫筆的style (Paint.Style.FILL填充,Paint.Style.STROKE描邊,Paint.Style.FILL_AND_STROKE填充加描邊  )paint.setStyle(Paint.Style.FILL);// 設置畫筆的顏色 這里只是繪制圖片感覺用不到就注釋掉了// paint.setColor(Color.RED);// paint.setStrokeWidth(10f);// decodeResourceBitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.girl);// 得到BitmapMatrix matrix = new Matrix();matrix.setScale(1.2f, 1.2f);canvas.drawBitmap(bitmap, matrix, paint);

效果圖如下:

3?drawBitmap(@NonNull Bitmap bitmap, @Nullable Rect src, @NonNull RectF dst, @Nullable Paint paint)

注意:src 本次繪制的原區域 dst 本次繪制的目標區域

paint.setAntiAlias(true);// 設置畫筆的style (Paint.Style.FILL填充,Paint.Style.STROKE描邊,Paint.Style.FILL_AND_STROKE填充加描邊  )paint.setStyle(Paint.Style.FILL);// 設置畫筆的顏色 這里只是繪制圖片感覺用不到就注釋掉了paint.setColor(Color.RED);paint.setStrokeWidth(10f);// decodeResourceBitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.girl);//src 本次繪制的原區域 dst 本次繪制的目標區域Rect src = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());canvas.drawRect(src, paint);Rect dst = new Rect(0, 0, 500, 300);canvas.drawBitmap(bitmap, src, dst, paint);

效果圖如下:

上面的方法我們估計在項目中沒怎么使用過,在自定義view 中我們為了方便統一的管理一般都是結合xml

來一起加載bitmap 下面也加載一個圖片看看

public class TestView extends View {// paint 初始化private Paint paint = new Paint();private Bitmap testBitmap;private int testBitmapResID;public TestView(Context context) {super(context);}public TestView(Context context, @Nullable AttributeSet attrs) {super(context, attrs);init(context, attrs);}public TestView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);init(context, attrs);}private void init(Context context, AttributeSet attrs) {paint.setAntiAlias(true);// 設置畫筆的style (Paint.Style.FILL填充,Paint.Style.STROKE描邊,Paint.Style.FILL_AND_STROKE填充加描邊  )paint.setStyle(Paint.Style.FILL);// 設置畫筆的顏色 這里只是繪制圖片感覺用不到就注釋掉了paint.setColor(Color.RED);paint.setStrokeWidth(10f);TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.FindImage);testBitmapResID = typedArray.getResourceId(R.styleable.FindImage_test_image, R.mipmap.girl);typedArray.recycle();}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);}@Overrideprotected void onLayout(boolean changed, int left, int top, int right, int bottom) {super.onLayout(changed, left, top, right, bottom);}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);testBitmap = BitmapFactory.decodeResource(getResources(), testBitmapResID);canvas.drawBitmap(testBitmap, 100, 100, paint);}}

attr.xml 的代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<resources><declare-styleable name="FindImage"><attr name="test_image" format="reference" /></declare-styleable>
</resources>

效果圖如下:

上面的這個繪制圖片的大家有沒有發現,我沒有放布局的xml 使用的默認的圖片,當然我們也可以不使用默認的的圖片

就是這個地方的

?下面修改下默認的圖片為android icon 然后在看下效果

自定義view:

public class TestView extends View {// paint 初始化private Paint paint = new Paint();private Bitmap testBitmap;private int testBitmapResID;public TestView(Context context) {super(context);}public TestView(Context context, @Nullable AttributeSet attrs) {super(context, attrs);init(context, attrs);}public TestView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);init(context, attrs);}private void init(Context context, AttributeSet attrs) {TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.FindView);testBitmap = BitmapFactory.decodeResource(getResources(), testBitmapResID);testBitmapResID = typedArray.getResourceId(R.styleable.FindView_test_image,R.drawable.ic_launcher_background);typedArray.recycle();}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);}@Overrideprotected void onLayout(boolean changed, int left, int top, int right, int bottom) {super.onLayout(changed, left, top, right, bottom);}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);paint.setStyle(Paint.Style.FILL);testBitmap = BitmapFactory.decodeResource(getResources(), testBitmapResID);canvas.drawBitmap(testBitmap, 100, 100, paint);}

att.xml

<?xml version="1.0" encoding="utf-8"?>
<resources><declare-styleable name="FindImage"><attr name="test_image" format="reference" /></declare-styleable>
</resources>

在布局中引用圖片:

<?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"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><com.hly.view.TestViewandroid:id="@+id/image"android:layout_width="wrap_content"android:layout_height="wrap_content"app:test_image="@mipmap/girl" /></LinearLayout>

這里注意test_image 是attr里面的name

效果圖如下:

總結

以上是生活随笔為你收集整理的Android 自定义View Canvas —— Bitmap的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。