自定义view 圆

首先这是一个仅仅继承了View的自定义,没有过多的方法


一: 写一个类,如MyView继承view


<span style="white-space:pre">	</span><span style="font-size:12px;"><strong>public class MyViewClice extends View {</strong></span>


如果不考虑padding和warp_content情况,直接重写onDraw()方法就行了,canvas画就可以了,如果考虑padding和warp_content,就要重写onMeasure()测量的方法,具体原因,我看了教程还是不懂,就这样先用着吧

<span style="white-space:pre">	</span><pre name="code" class="java"><span style="color:#ff6600;"><span style="font-size:14px;">package com.ljg.viewzidingyi.app;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class MyViewClice extends View {

	
	private Paint pa=new Paint(Paint.ANTI_ALIAS_FLAG);
	private int couler=Color.RED;
	
	public MyViewClice(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
//		//获取自定义属性的集合
//		TypedArray ta=context.obtainStyledAttributes(attrs, R.styleable.nimadewojiucaole);
//		//获取自定义属性集合中的子元素
//		int color=ta.getColor(R.styleable.nimadewojiucaole_circle_color, Color.BLUE);
//		//释放资源
//		ta.recycle();
		init();
	}

	private void init() {
		// TODO Auto-generated method stub
		pa.setColor(couler);
	}

	
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		/**
		 * 对wart—count进行处理
		 */
		int widthSpecMode=MeasureSpec.getMode(widthMeasureSpec);
		int widthSpecSize=MeasureSpec.getSize(widthMeasureSpec);
		int heightSpecMode=MeasureSpec.getMode(heightMeasureSpec);
		int heightSpecSize=MeasureSpec.getSize(heightMeasureSpec);
		
		//给一个默认值,可以改200
		if(widthSpecMode==MeasureSpec.AT_MOST&&heightSpecMode==MeasureSpec.AT_MOST)
		{
			setMeasuredDimension(200, 200);
		}else if(widthSpecMode==MeasureSpec.AT_MOST)
		{
			setMeasuredDimension(200, heightSpecSize);
		}else if(heightSpecMode==MeasureSpec.AT_MOST)
		{
			setMeasuredDimension(widthSpecSize, 200);
		}
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		
		int padingl=getPaddingLeft();
		int padingr=getPaddingLeft();
		int padingt=getPaddingLeft();
		int padingb=getPaddingLeft();
				
		//得到宽和高之后,减去padding
		int width=getWidth()-padingl-padingr;
		int height=getHeight()-padingt-padingb;
		//三元
		int radius=width<height?width/4:height/4;
		//绘制圆形
		canvas.drawCircle(width/2+padingl, padingb+height/2, radius, pa);
		
	}
	
	
	
}</span>
</span>
第二步 :在xml中调用自定义的view

 
</pre><pre code_snippet_id="1581444" snippet_file_name="blog_20160218_4_6270147" name="code" class="html"><span style="white-space:pre">	</span><pre name="code" class="java"><span style="font-size:14px;color:#3366ff;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <View
        android:id="@+id/sdsd"
        android:layout_width="match_parent"
        android:layout_height="6dp" >
    </View>

    <com.ljg.viewzidingyi.app.MyViewClice
        android:id="@+id/id_main_myveiw"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="#000000" >
    </com.ljg.viewzidingyi.app.MyViewClice>

    <View
        android:id="@+id/sds"
        android:layout_width="match_parent"
        android:layout_height="6dp" >
    </View>

    <com.ljg.viewzidingyi.app.MyViewClice
        android:id="@+id/id_main_myveiw"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_margin="20dp"
        android:background="#000000" >
    </com.ljg.viewzidingyi.app.MyViewClice>

    <View
        android:id="@+id/sdsds"
        android:layout_width="match_parent"
        android:layout_height="6dp" >
    </View>

    <com.ljg.viewzidingyi.app.MyViewClice
        android:id="@+id/id_main_myveiw"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="#000000" 
        android:padding="20dp"
        >
    </com.ljg.viewzidingyi.app.MyViewClice>

    <View
        android:id="@+id/sdssda"
        android:layout_width="match_parent"
        android:layout_height="6dp" >
    </View>

    <com.ljg.viewzidingyi.app.MyViewClice
        android:id="@+id/id_main_myveiw"
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:padding="30dp"
        android:background="#000000" >
    </com.ljg.viewzidingyi.app.MyViewClice>

</LinearLayout></span>



点击下载Dome:

http://download.csdn.net/detail/jiajia1112223/9435388




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值