首页 > 其他 > 详细

自定义属性 view

时间:2016-04-09 18:26:44      阅读:176      评论:0      收藏:0      [点我收藏+]

首先自定义一个圆,相信以前的学习大家都会画圆,在values下写一些自定义的属性

 

package com.exaple.day01rikao;

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.MotionEvent;
import android.view.View;

public class Myview extends View {
int radius;
int color;
private final static String Nam = "chen";

public Myview(Context context, AttributeSet attrs) {
super(context, attrs);
/*
* TypedArray ta = context.obtainStyledAttributes(attrs,
* R.styleable.Myview); radius = ta.getInt(R.styleable.Myview_radius,
* 0); color = ta.getInt(R.styleable.Myview_mycolor, 0);

较难的一种方式
*/
radius = attrs.getAttributeIntValue(Nam, "radius", 0);
color = attrs.getAttributeIntValue(Nam, "mycolor", 0);

简单的一中方式
}

@Override
protected void onDraw(Canvas canvas) {
Paint pa = new Paint();
Paint pa1 = new Paint();
float wi = canvas.getWidth();
float he = canvas.getHeight();
pa.setColor(color);
pa1.setColor(Color.WHITE);

canvas.drawColor(Color.WHITE);
canvas.drawCircle(wi / 2, he / 2, radius, pa);
/* canvas.drawCircle(wi / 2, he / 2, 55, pa1); */
/* canvas.drawText("hhhhhhhhhhhhhh", wi / 4, he / 4, pa); */
super.onDraw(canvas);
}

/*
* @Override public boolean onTouchEvent(MotionEvent event) { wi =
* event.getX(); he = event.getY(); this.invalidate();
*
* return true; }
*/

 

}

 

自定义属性 view

原文:http://www.cnblogs.com/jsonfan/p/5371978.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!