Android 自定义View

2018-07-25  本文已影响52人  01_小小鱼_01

Android 绘图基础:Canvas、Paint、Color
自定义View 需要集成View组件,并且重写其onDraw(Canvas canvas)就可以了。

public class MyView extends View {
 
    public MyView(Context context) {
        super(context);
    }
    
    public void onDraw(Canvas canvas) {
        Paint paint_circle = new Paint();
        paint_circle.setAntiAlias(true);
        paint_circle.setColor(Color.BLUE);
        paint_circle.setStyle(Style.STROKE);
        paint_circle.setStrokeWidth(10);
        canvas.drawCircle(110,150,60, paint_circle);

        Paint paint_string = new Paint();
        paint_string.setColor(Color.BLUE);
        paint_string.setTextSize(20);
        canvas.drawText("Welcome to Beijing", 245, 310, paint_string);
        
        //绘制福娃图片
        canvas.drawBitmap(BitmapFactory.decodeResource(getResources(),
                        R.drawable.fuwa), 35, 340, paint_line);
    }
}
Canvas类

Canvas类可以用来实现各种图形的绘制工作常用图形的方法如下:

Paint类

要绘制图形,首先得调整画笔,按照自己的开发需要设置画笔的相关属性。Pain类的常用属性设置方法如下:

Bitmap、BitmapDrawable、BitmapFactory类

Bitmap代表一张位图,BitmapDrawable里面封装类一个Bitmap对象。

BitmapDrawable drawable = new BitmapDrawable(bitmap);
Bitmap bitmap = drawable.getBitmap();
bitmap.recycle();

BitmapFactory是一个工具类,提供了大量的方法。

实例
/*
 <com.xc.xcskin.view.XCRoundImageView
        android:id="@+id/roundImageView"  
        android:layout_centerInParent="true" 
        android:layout_width="200dp"   
        android:layout_height="200dp"
        android:src="@drawable/roundimageview"
          />
*/

public class XCRoundImageView extends ImageView{

    private Paint paint ;
    
    public XCRoundImageView(Context context) {  
        this(context,null);  
    }  
  
    public XCRoundImageView(Context context, AttributeSet attrs) {  
        this(context, attrs,0);  
    }  
  
    public XCRoundImageView(Context context, AttributeSet attrs, int defStyle) {  
        super(context, attrs, defStyle); 
        paint = new Paint();
        
    }  
  
    @Override  
    protected void onDraw(Canvas canvas) {  
        Drawable drawable = getDrawable();  
        if (null != drawable) {  
            Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();  
            Bitmap b = getCircleBitmap(bitmap, 14);  
            final Rect rectSrc = new Rect(0, 0, b.getWidth(), b.getHeight());  
            final Rect rectDest = new Rect(0,0,getWidth(),getHeight());
            paint.reset();  
            canvas.drawBitmap(b, rectSrc, rectDest, paint);  
        } else {  
            super.onDraw(canvas);  
        }  
    }  
  
    private Bitmap getCircleBitmap(Bitmap bitmap, int pixels) {  
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),  
                bitmap.getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(output);
          
        final int color = 0xff424242;
       
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());  
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        int x = bitmap.getWidth();
        
        canvas.drawCircle(x / 2, x / 2, x / 2, paint);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
        return output;
    }
}

更多内容:
1. Android 自定义View修炼-Android实现圆形、圆角和椭圆自定义图片View(使用BitmapShader图形渲染方法)

上一篇下一篇

猜你喜欢

热点阅读