1 package com.example.user_defined_view; 2 3 import android.annotation.SuppressLint; 4 import android.content.Context; 5 import android.graphics.Bitmap; 6 import android.graphics.BitmapFactory; 7 import android.graphics.Canvas; 8 import android.graphics.Color; 9 import android.graphics.Paint; 10 import android.graphics.Rect; 11 import android.util.AttributeSet; 12 import android.view.View; 13 14 @SuppressLint("DrawAllocation") 15 public class Bitmap_View extends View { 16 17 public Bitm
ap_View(Context context, AttributeSet attrs) { 18 super(context, attrs); 19 } 20 @SuppressLint("DrawAllocation") 21 @Override 22 protected void onDraw(Canvas canvas) { 23 Bitmap bitmap = BitmapFactory.decodeResource(super.getResources(), 24 R.drawable.ic_launcher);// 找到bitmap对象 25 Paint paint = new Paint(); 26 paint.setAntiAlias(true);// 消除龋齿 27 canvas.drawBitmap(bitmap, new Rect(30, 50, 200, 200), new Rect(30, 50, 200, 200), paint); 28 29 30 } 31 32 }
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 tools:context=".MainActivity" > 6 7 <com.example.user_defined_view.Bitmap_View 8 android:layout_width="fill_parent" 9 android:layout_height="wrap_content" /> 10 11 </RelativeLayout>
浙公网安备 33010602011771号