自定义属性 view
首先自定义一个圆,相信以前的学习大家都会画圆,在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; }
*/
}
在values 下面新建一个attrs文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Myview">-----类名
<attr name="radius" format="integer"></attr>
<attr name="mycolor" format="color|reference"></attr>
</declare-styleable>
</resources>
Mainactivity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:fan="chen"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<com.exaple.day01rikao.Myview
android:layout_width="wrap_content"
android:layout_height="wrap_content"
fan:mycolor="#f00"
fan:radius="100" />
</RelativeLayout>
自定义属性 view的更多相关文章
- 微信小程序开发常用方法
1.函数中访问data中的数据 _this.setData({ // 日历数据 signList: dataList, // 当前日期 todayDay: str }) 2.if判断 wx:if=&q ...
- Android自定义View自定义属性
1.引言 对于自定义属性,大家肯定都不陌生,遵循以下几步,就可以实现: 自定义一个CustomView(extends View )类 编写values/attrs.xml,在其中编写styleabl ...
- View (二) 自定义属性
主要有三种方法可以实现自定义属性. 方法一:不使用命名空间,不使用attrs.xml文件.通过attrs.getAttributeResourceValue方法拿到属性值 方法二: 使用命名空间, 不 ...
- Android 高手进阶之自定义View,自定义属性(带进度的圆形进度条)
Android 高手进阶(21) 版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请注明地址:http://blog.csdn.net/xiaanming/article/detail ...
- Android 自定义View修炼-自定义View-带百分比进度的圆形进度条(采用自定义属性)
很多的时候,系统自带的View满足不了我们功能的需求,那么我们就需要自己来自定义一个能满足我们需求的View,自定义View我们需要先继承View,添加类的构造方法,重写父类View的一些方法,例如o ...
- Android自定义View(二、深入解析自定义属性)
转载请标明出处: http://blog.csdn.net/xmxkf/article/details/51468648 本文出自:[openXu的博客] 目录: 为什么要自定义属性 怎样自定义属性 ...
- Android初级教程初谈自定义view自定义属性
有些时候,自己要在布局文件中重复书写大量的代码来定义一个布局.这是最基本的使用,当然要掌握:但是有些场景都去对应的布局里面写对应的属性,就显得很无力.会发现,系统自带的控件无法满足我们的要求,这个时候 ...
- Android 自定义View二(深入了解自定义属性attrs.xml)
1.为什么要自定义属性 要使用属性,首先这个属性应该存在,所以如果我们要使用自己的属性,必须要先把他定义出来才能使用.但我们平时在写布局文件的时候好像没有自己定义属性,但我们照样可以用很多属性,这是为 ...
- 手机安全卫士——在设置中心 自定义view和自定义属性
自定义组合控件 1. 自定义一个View, 继承ViewGroup,比如RelativeLayout,此文中是SettingItemView 2. 编写组合控件的布局文件,在自定义的View中加载 ...
随机推荐
- asp.net分割字符串的几种方法
在编写程序中,经常要用到分割的方法来处理一些字符串.这里总结了几种常用的分割方法: 1.最简单最常用的方法,以一个指定的字符进行的分割 string s="abcdeabcdeabcde& ...
- 李洪强漫谈iOS开发[C语言-049]-猜数字游戏
- scala - Enumeration 诡异问题
object WeekDay extends Enumeration { type WeekDay = Value val Mon, Tue, Wed, Thu, Fri, Sat, Sun = Va ...
- Learn ZYNQ Programming(1)
GPIO LED AND KEY: part1:gpio leds and gpio btns combination. (include 1~4) part2:use gpio btns inter ...
- C++_static与非static成员(函数)
static与非static成员(函数) <C++ Primer>第4版399页: 对于特定类类型的全体对象而言,访问一个全局变量有时是必要的.然而,全局变量会破坏封装:对象需要支持特定 ...
- linux卸载mysql,apache,php
卸载Mysql 1.查找以前是否装有mysql 命令:rpm -qa|grep -i mysql 可以看到mysql的包: mysql-3.23.58-9php-mysql-4.3.4-11mod_a ...
- Python一般错误
1. IndentationError: unindent does not match any outer indentation level 格式对齐的问题.Python对空格和Tab有严格区别
- 在win7下安装unbuntu系统
1.分盘 准备EasyBCD,ubuntu操作系统 配置EasyBCD,安装ubuntu http://www.linuxidc.com/Linux/2014-04/100369.htm http:/ ...
- iOS 3D touch 使用技巧
第一个 在桌面中3d Touch 打开菜单 由于本人纯属代码党,本次实现方法也只使用代码实现 到达到这个效果并不难,只需要在appdelegate中实现以下代码即可 ,当然也有缺点,就是这个app没运 ...
- php基础篇-二维数组排序 array_multisort
原文:php基础篇-二维数组排序 array_multisort 对2维数组或者多维数组排序是常见的问题,在php中我们有个专门的多维数组排序函数,下面简单介绍下: array_multisort(a ...