自定义圆环随机数

本文介绍了如何在Android中自定义一个圆环视图,并在用户触摸时动态更新内部显示的随机数。通过RingView类实现圆环的绘制,包括内圆、圆环和文字,同时在onTouchEvent事件中生成并显示随机数。
摘要由CSDN通过智能技术生成
main布局(自定义view控件)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.example.a_zidingiyiview_demo.RingView
        android:layout_width="match_parent"
        android:layout_height="match_parent"></com.example.a_zidingiyiview_demo.RingView>

</LinearLayout>
自定义view代码
public class RingView extends View {

    private final Paint paint;
    private final Context context;
    private int i=1234;

    public RingView(Context context) {
        this(context,null);
    }

    public RingView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        this.paint = new Paint();
        this.paint.setAntiAlias(true); //消除锯齿
        this.paint.setStyle(Paint.Style.STROKE); //绘制空心圆
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        int center = getWidth()/2;
        int innerCircle = dip2px(context, 40); //设置内圆半径
        int ringWidth = dip2px(context, 5); //设置圆环宽度

        //绘制内圆
        this.paint.setARGB(155, 167, 190, 206);
        this.paint.setStrokeWidth(30);
        this.paint.setColor(Color.YELLOW);
        canvas.drawCircle(center,center, innerCircle, this.paint);


        //绘制文字
        Paint  textPaint = new Paint();
        textPaint.setColor(Color.BLACK);
        textPaint.setStyle(Paint.Style.FILL);
        textPaint.setTextSize(25);
        canvas.drawText(String.valueOf(i),330,370,textPaint);

        //绘制圆环
     /*   this.paint.setARGB(255, 212 ,225, 233);
       this.paint.setStrokeWidth(2);
        this.paint.setColor(Color.BLUE);
        canvas.drawCircle(center,center, innerCircle+1+ringWidth/2, this.paint);

        //绘制外圆
       /* this.paint.setARGB(155, 167, 190, 206);
        this.paint.setStrokeWidth(10);
        this.paint.setColor(Color.RED);
        canvas.drawCircle(center,center, innerCircle+ringWidth, this.paint);*/


        super.onDraw(canvas);
    }


    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                //随机数的工具
                Random random = new Random();
                //做计算
                i=1001+(int)(Math.random()*1000);
//                count++;
                invalidate();
                break;
        }
        return super.onTouchEvent(event);
    }
}
如有雷同 不胜荣幸!!!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值