Android绘图API自定义View(三)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/dpl12/article/details/82945562

运行效果:

自定义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

猜你喜欢

转载自blog.csdn.net/dpl12/article/details/82945562
今日推荐