用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - android代码库

自定义view

2017-04-05 作者: 云代码会员举报

[android]代码库

public class CircleView extends View {

    private int bigRedius;
    private int smallRedius;
    private String text;
    private int textSize;
    private int color;
   Paint paint;

    public CircleView(Context context) {
        super(context);
        initView(context, null);
    }

    public CircleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {

        //获取自定义属性
        TypedArray typearray = context.obtainStyledAttributes(attrs,R.styleable.CircleView);
        /**外圆半径*/
        bigRedius = (int)typearray.getDimension(R.styleable.CircleView_BigRedius,150);
        /** 内圆半径*/
        smallRedius = (int) typearray.getDimension(R.styleable.CircleView_smallRedius,100);
        /** 字体*/
        text = typearray.getString(R.styleable.CircleView_text);
        /** 字体大小*/
        textSize = (int) typearray.getDimension(R.styleable.CircleView_textSize,50);
        /** 圆环颜色*/
        color = typearray.getColor(R.styleable.CircleView_color, Color.YELLOW);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //重新侧量
        setMeasuredDimension(bigRedius*2,bigRedius*2);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //初始化画笔
        paint = new Paint();
        //设置画笔颜色
        paint.setColor(color);
        //绘制外圆
        canvas.drawCircle(getWidth()/2,getHeight()/2,bigRedius,paint);
        paint.setColor(Color.WHITE);
        //绘制内圆
        canvas.drawCircle(getWidth()/2,getHeight()/2,smallRedius,paint);
        //设置字体大小 颜色
        paint.setTextSize(textSize);
        paint.setColor(Color.BLACK);
        //绘制文本内容  首先获取字体的宽,高
        Rect rect = new Rect();
        paint.getTextBounds(text,0,text.length(),rect);
        //获取文本的宽高
        int textWidth = rect.width();
        int textHeight = rect.height();

        //进行绘制
        canvas.drawText(text, getWidth()/2 - (textWidth /2), getHeight()/2 + (textHeight/2),paint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                //获取按下时的位置
                int downX = (int) event.getX();
                int downY = (int) event.getY();
                //对位置进行判断
                judegDown(downX,downY);
                break;
        }
        return true;
    }
    private void judegDown(int downX, int downY) {
        //根据勾股定理获取点击位置到圆中心点的距离来判断点击的位置
        int absX = Math.abs(getWidth()/2 - downX);
        int absY = Math.abs(getHeight()/2 - downY);
        double sqrt = Math.sqrt(absX * absX + absY * absY);

        if(sqrt <= bigRedius){
            Random rand = new Random();
            text =""+rand.nextInt(9999);
            postInvalidate();
        }
    }
}
/////////////////////////////////////////
<RelativeLayout 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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.bwei.wenhuan20170213.MainActivity">

  <com.bwei.wenhuan20170213.view.CircleView
      android:id="@+id/circleview"
      android:layout_centerInParent="true"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      app:text="1234"
      app:BigRedius="60dp"
      app:smallRedius="30dp"
      app:textSize="15sp"
      app:color="@color/yellow"/>
</RelativeLayout>
//////////////////////
attrs
<resources>
    <declare-styleable name="CircleView">
        <attr name="color" format="color"/>
        <attr name="BigRedius" format="dimension"/>
        <attr name="smallRedius" format="dimension"/>
        <attr name="text" format="string"/>
        <attr name="textSize" format="dimension"/>
    </declare-styleable>
</resources>


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...