android自定义view

来源:互联网 发布:java动态数组赋值 编辑:程序博客网 时间:2024/06/10 17:14
这个是class类里面的内容
不同的类包装了不同的class内容
其中还用到了values中的attrs的xml
<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="Mytext">        <attr name="titletext" format="string"></attr>        <attr name="titlecolor" format="color"></attr>        <attr name="titlesize" format="dimension"></attr>    </declare-styleable></resources>

public class TextViews extends View{    public TextViews(Context context) {        super(context);    }    public TextViews(Context context, @Nullable AttributeSet attrs) {        super(context, attrs);    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        Paint paint = new Paint();        paint.setAntiAlias(true);        paint.setTextSize(24);        paint.setStrokeWidth(1);        paint.setColor(Color.BLACK);        paint.setStyle(Paint.Style.STROKE);        Path path = new Path();        RectF rectF = new RectF(50,100,300,300);        canvas.drawOval(rectF,paint);        path.addOval(rectF, Path.Direction.CW );        canvas.drawTextOnPath("1508B班",path,0,-50,paint);    }}

public class CircleView extends View {    private int color;    private String string;    private Paint paint;    public CircleView(Context context) {        super(context);    }    public CircleView(Context context, @Nullable AttributeSet attrs) {        super(context, attrs);        init(context, attrs);    }    private void init(Context context, @Nullable AttributeSet attrs) {        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.Mytext);        color = typedArray.getColor(R.styleable.Mytext_titlecolor, Color.RED);        string = typedArray.getString(R.styleable.Mytext_titletext);        typedArray.recycle();        paint = new Paint();        paint.setAntiAlias(true);        paint.setStrokeWidth(2);        paint.setTextSize(28);        paint.setColor(color);        paint.setStyle(Paint.Style.STROKE);    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        Path path = new Path();        path.moveTo(100,100);        path.lineTo(300,100);        canvas.drawTextOnPath(string,path,0,0,paint);    }}

public class MyViews extends View { 
    public MyViews(Context context) {        super(context);    }    public MyViews(Context context, @Nullable AttributeSet attrs) {        super(context, attrs);    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        Paint paint = new Paint();        paint.setColor(Color.RED);        paint.setStrokeWidth(4);        paint.setStyle(Paint.Style.FILL);        paint.setAntiAlias(true);        //回执圆形        Drawable drawable = getResources().getDrawable(R.mipmap.aa);        Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();        BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);        paint.setShader(bitmapShader);        canvas.drawCircle(60, 100, 50, paint);        //绘制矩形        canvas.drawRect(10, 160, 60, 210, paint);        //绘制圆角矩形        RectF rectF = new RectF(10, 220, 110, 260);        canvas.drawRoundRect(rectF, 10, 10, paint);        //绘制扇形        RectF rectF1 = new RectF(10,270,210,350);        canvas.drawArc(rectF1,120,120,true,paint);        //绘制点        canvas.drawPoint(20,420,paint);         //起始位置        Path path = new Path();        path.moveTo(60,450);        path.lineTo(20,490);        path.lineTo(100,490);        path.close();        canvas.drawPath(path,paint);    }}
<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.example.myexamview.MainActivity"><com.example.myexamview.MyViews    android:layout_width="wrap_content"    android:layout_height="wrap_content"    app:titletext="1508B"    app:titlecolor="@color/colorAccent"    /></android.support.constraint.ConstraintLayout>