Android自定义View实例

  • 本人也是才学自定义View
  • 大佬勿吐槽
    样品
    代码MyView.java
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class MyView extends View {

    private Paint mPaint;
    private Paint mPaintBorder;

    public MyView(Context context) {
        super(context);
        init();
    }

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        mPaint = new Paint();
        mPaint.setAntiAlias(true);          //抗锯齿
        mPaint.setColor(getResources().getColor(android.R.color.holo_purple));//画笔颜色
        mPaint.setStyle(Paint.Style.FILL);  //画笔风格
        
        
        mPaintBorder = new Paint();
        mPaintBorder.setAntiAlias(true);          //抗锯齿
        mPaintBorder.setColor(getResources().getColor(android.R.color.holo_purple));//画笔颜色
        mPaintBorder.setStyle(Paint.Style.STROKE);  //画笔风格
      //  mPaintBorder.setTextSize(100);             //绘制文字大小,单位px
        mPaintBorder.setStrokeWidth(20);           //画笔粗细
    }

    //重写该方法,在这里绘图
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        
        mPaint.setColor(Color.parseColor("#ff631b9a"));
        canvas.drawRect(0,0,getWidth(),getHeight(),mPaint);
        
        mPaint.setColor(getResources().getColor(android.R.color.holo_purple));
        canvas.drawCircle(0, 0, 100, mPaint);           //画实心圆
        canvas.drawCircle(0,getBottom(),100,mPaint);
        canvas.drawCircle(getRight(),0,100,mPaint);
        canvas.drawCircle(getRight(),getBottom(),100,mPaint);
      //  canvas.drawRect(0, 0, getRight(), getBottom(), mPaint);            //画矩形
        drawBorder(canvas);
        
        
        
    }
    
    private void drawBorder(Canvas canvas) {
     /*   canvas.scale(5,5);
        canvas.drawLine(0, 0, this.getWidth() - 3, 0, mPaintBorder);  
        canvas.drawLine(0, 0, 0, this.getHeight() - 3, mPaintBorder);  
        canvas.drawLine(this.getWidth() - 3, 0, this.getWidth() - 3, this.getHeight() - 3, mPaintBorder);  
        canvas.drawLine(0, this.getHeight() - 3, this.getWidth() - 3, this.getHeight() - 3, mPaintBorder);  
        Toast.makeText(getContext(),getWidth()+";)"+getHeight(),Toast.LENGTH_SHORT).show();*/
        canvas.drawRect(0,0,getWidth(),getHeight(),mPaintBorder);
    }

} 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值