• 自己定义View之绘制圆环


    一、RingView

    自己定义的view,构造器必须重写,至于重写哪个方法,參考例如以下:

    ①假设须要改变View绘制的图像,那么须要重写OnDraw方法。(这也是最经常使用的重写方式。)

    ②假设须要改变view的大小,那么须要重写OnMeasure方法。

    ③假设须要改变View的(在父控件的)位置,那么须要重写OnLayout方法。

    ④依据上面三种不同的须要你能够组合出多种重写方案,你懂的。

    凝视信息代码中比較具体。

    package com.example.customerviewdemo2;
    
    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Paint;
    import android.util.AttributeSet;
    import android.view.View;
    
    public class RingView extends View {
    
    	private final  Paint paint;
    	private final Context context;
    	
    	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, 83); //设置内圆半径
    		int ringWidth = dip2px(context, 10); //设置圆环宽度
    		
    		//绘制内圆
    		this.paint.setARGB(155, 167, 190, 206);
    		this.paint.setStrokeWidth(10);//设置内圆的厚度
    		canvas.drawCircle(center,center, innerCircle, this.paint);//以该圆为半径向内外扩展至厚度为10px
    		
    		//绘制圆环,设置圆环的颜色改动画笔的颜色
    //		this.paint.setARGB(255, 212 ,225, 233);
    		this.paint.setARGB(255, 255, 0, 0);
    		this.paint.setStrokeWidth(ringWidth);//设置圆环宽度
    		canvas.drawCircle(center,center, innerCircle+1+ringWidth/2, this.paint);//圆环宽度为中间圆
    		
    		//绘制外圆
    		this.paint.setARGB(155, 167, 190, 206);
    		this.paint.setStrokeWidth(2);
    		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);
    	}
    }
    
    

    二、引用该View的代码

    <RelativeLayout 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"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity" >
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />
    
        <com.example.customerviewdemo2.RingView
            android:layout_width="300dp"
            android:layout_height="300dp" >
        </com.example.customerviewdemo2.RingView>
    
    </RelativeLayout>
    三、效果例如以下



  • 相关阅读:
    为什么数据库连接很消耗资源 转
    在Eclipse中编写servlet时出现"The import javax.servlet cannot be resolved" 问题解决办法
    multipart/form-data post 方法提交表单,后台获取不到数据
    java 数据库查询Date类型字段 没有了时分秒 全为 00 的解决办法
    Eclipse/myEclipse 代码提示/自动提示/自动完成设置(转)
    PL/SQL破解方法(不需要注册码)
    Java
    tomcat开发环境配置
    文件操作权限
    Java版office文档在线预览
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/4487358.html
Copyright © 2020-2023  润新知