美文网首页
Android 自定义View

Android 自定义View

作者: 01_小小鱼_01 | 来源:发表于2018-07-25 21:46 被阅读52次

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类可以用来实现各种图形的绘制工作常用图形的方法如下:

  • 绘制直线:canvas.drawLine(float startX, float startY, float stopX, float stopY, Paint paint);
  • 绘制矩形:canvas.drawRect(float topLeftX, float topLeftY, float rightBottomX, float rightBottomY, Paint paint);
  • 绘制圆形:canvas.drawCircle(float cx, float cy, float radius, Paint paint);
  • 绘制字符:canvas.drawText(String text, float x, float y, Paint paint);
  • 绘制图形:canvas.drawBitmap(Bitmap bitmap, float left, float top, Paint paint);
Paint类

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

  • setAntiAlias(); //设置画笔的锯齿效果
  • setColor(); //设置画笔的颜色
  • setARGB(); //设置画笔的A、R、G、B值
  • setAlpha(); //设置画笔的Alpha值
  • setTextSize(); //设置字体的尺寸
  • setStyle(); //设置画笔的风格(空心或实心)
  • setStrokeWidth(); //设置空心边框的宽度
  • getColor(); //获取画笔的颜色
Bitmap、BitmapDrawable、BitmapFactory类

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

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

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

  • public static Bitmap decodeFile (String pathName)
  • public static Bitmap decodeByteArray (byte[] data, int offset, int length)
  • public static Bitmap decodeStream (InputStream is)
实例
  • 圆角图片
/*
 <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图形渲染方法)

相关文章

网友评论

      本文标题:Android 自定义View

      本文链接:https://www.haomeiwen.com/subject/ynpmmftx.html