Android绘图API自定义View(三)

运行效果:

自定义View类:CustomViewDraw.java

public class CustomViewDraw extends View {
    private Paint paint;
    private float degrees=0;
    public CustomViewDraw(Context context) {
        super(context);
        initProperties();
    }

    public CustomViewDraw(Context context,AttributeSet attrs) {
        super(context, attrs);
        initProperties();
    }

    public CustomViewDraw(Context context,AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initProperties();
    }
    private void initProperties(){//初始化数据
        paint=new Paint();
        paint.setColor(Color.RED);
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);

        canvas.save();//保存当前画布状态
        canvas.translate(400,400);//view的位置
        canvas.rotate(degrees,50,50);//视图旋转
        canvas.drawRect(0,0,100,100,paint);//绘制的图形
        degrees++;
        canvas.restore();//恢复画布状态
        invalidate();//视图重绘,结束视图
    }
}

View视图布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <com.example.custombutton.CustomViewDraw
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

参考博客:

https://www.jianshu.com/p/afa06f716ca6

https://www.jianshu.com/p/f69873371763

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值