持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第9天,点击查看活动详情
目录
什么是attr,实现自定义view
Attr基本概念
Attr :单词的意思是属性的意思(但是这里的属性和xml控件中的属性不是一个意思,不要混淆!Attr说是属性只是说的是它的单词的意思是属性),我们是通过Attr文件来定义我们控件中所使用的属性的,这样说可能大家会有一迷惑,那么来举个栗子:
比如我们在控件中最多使用的 layout_width 属性;
编辑
编辑
实现attr 自定义view
layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--引用自定义view必须是包名.类名-->
<com.boyoi.ysj.custom.one.view.YView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
custom:viewText="这是我的自定义View"
custom:viewTextColor="@android:color/holo_red_dark"
custom:viewTextBg="@android:color/holo_blue_dark"
custom:viewTextSize="18sp" />
</LinearLayout>
自定义控件View
/**
* Created by yishujun on 16/6/3.
*/
public class YView extends View {
private Context mContext;
//文本
private String mText;
//文本的颜色
private int mTextColor;
//文本的大小
private int mTextSize;
//文本的背景
private int mTextBg;
//绘制时控制文本绘制的范围
private Rect mBound;
//绘制文本画笔
private Paint mPaint;
public YView(Context context) {
this(context, null);
}
public YView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public YView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.mContext = context;
typedYView(attrs, defStyleAttr);
}
/**
* 获得我们所定义的自定义样式属性
*
* @param attrs
* @param defStyleAttr
*/
private void typedYView(AttributeSet attrs, int defStyleAttr) {
//获得我们所定义的自定义样式属性
//final Resources.Theme theme = mContext.getTheme();
//TypedArray a = theme.obtainStyledAttributes(attrs,R.styleable.YText, defStyleAttr, 0);
//获取自定义属性值的方式一般情况分为两种:styleable组 和 直接获取attr属性
//这里获取的属性用的styleable组,同时我也建议用这种方式方便规范attrs文件,
//另外一种获取方式如下,当然也可以一个个获取属性值,这里不再演示
//int[] custom = {R.attr.viewText, R.attr.viewTextSize};
//TypedArray a = mContext.obtainStyledAttributes(attrs, custom);
TypedArray a = mContext.getTheme().obtainStyledAttributes(attrs, R.styleable.YText, defStyleAttr, 0);
int n = a.getIndexCount();
for (int i = 0; i <= n; i++) {
int attr = a.getIndex(i);
switch (attr) {
//注意获取属性的方式为 styleable的名称_属性名称
case R.styleable.YText_viewText:
mText = a.getString(attr);
break;
case R.styleable.YText_viewTextColor:
// 默认颜色设置为黑色
mTextColor = a.getColor(attr, Color.BLACK);
break;
case R.styleable.YText_viewTextSize:
// 默认设置为16sp,TypeValue也可以把sp转化为px
mTextSize = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_SP, 18, getResources().getDisplayMetrics()));
break;
case R.styleable.YText_viewTextBg:
// 默认颜色设置为黄色
mTextBg = a.getColor(attr, Color.YELLOW);
//但是在这里上面的那种取值方式就是取不到值哦,因为返回后的attr没有YText_viewTextBg,原因是因为我刚装逼了一下,所以我们要单独去取值
break;
}
}
//记得在这里单独取出文本的背景mTextBg值哦,因为上面的mTextBg取不到值哦
mTextBg = a.getColor(R.styleable.YText_viewTextBg, Color.YELLOW);
//回收资源
a.recycle();
//新建画笔对象
mPaint = new Paint();
//设置画笔
mPaint.setTextSize(mTextSize);
mBound = new Rect();
//设置画笔绘制文字及相关区域
mPaint.getTextBounds(mText, 0, mText.length(), mBound);
this.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mText = "你摸了我一下";
//刷新画布
postInvalidate();
}
});
}
@Override
protected void onDraw(Canvas canvas) {
//设置画布颜色即文字背景色
mPaint.setColor(mTextBg);
//绘制背景,全屏
canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), mPaint);
//设置文字颜色
mPaint.setColor(mTextColor);
//绘制文字
canvas.drawText(mText, getWidth() / 2 - mBound.width() / 2, getHeight() / 2 + mBound.height() / 2, mPaint);
}
}
attr 属性
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--name 是自定义属性名,一般采用驼峰命名,可以随意。 format 是属性的单位-->
<attr name="viewText" format="string" />
<attr name="viewTextColor" format="color" />
<attr name="viewTextSize" format="dimension" />
<!--name 是自定义控件的类名-->
<declare-styleable name="YText">
<attr name="viewText" />
<attr name="viewTextColor" />
<attr name="viewTextSize" />
<!--注意:一般情况是按照上面这样写,把属性单独定义在上面,然后在styleable这里面引用,但是我要装一下逼,就单独混写在里面了下,取值的时候就需要单独去取名称才能取到值不然是取不到值-->
<attr name="viewTextBg" format="color" />
</declare-styleable>
</resources>
定时器
public class TimerTest{
public static void main(String[] args){
Timer timer = new Timer();
timer.schedule(new MyTask(), 1000, 2000);//在1秒后执行此任务,每次间隔2秒执行一次,如果传递一个Data参数,就可以在某个固定的时间执行这个任务.
while(true){//这个是用来停止此任务的,否则就一直循环执行此任务
try{
int in = System.in.read();
if(in == 's'){
timer.cancel();//使用这个方法退出任务
break;
}
} catch (IOException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
static class MyTask extends java.util.TimerTask{
public void run(){
System.out.println("________");
}
}
}
sendMessageDelayed
sendMessageDelayed是如何实现延时发送消息的? sendMessageDelayed是通过阻塞来达到了延时发送消息的结果,那么会不会阻塞新添加的Message?
android Module之间数据传递
(1)在子module创建回调接口(参数可变) public interface OnChangeLisener { void onChanged(Date date); } (2)在子module 实现类设置接口回调 //设置选择回调 public void setOnChangeLisener(OnChangeLisener onChangeLisener) { this.onChangeLisener = onChangeLisener; } 实现方法
onChangeLisener.onChanged(DateUtils.getDate(year, moth, day, hour, minut)); (3)在app module设置接口回调即可
private OnChangeLisener onChangeLisener; onChangeLisener =new OnChangeLisener() { @Override public void onChanged(Date date) { String messge = ""; } }; DatePickDialog dialog = new DatePickDialog(this); //设置选择回调 dialog.setOnChangeLisener(onChangeLisener);
方法二:使用 sharedpreferences 在一个module中保存数据
SharedPreferences sp = getSharedPreferences("sp_demo", Context.MODE_PRIVATE);sp.edit().
putString("name", "梅西").putInt("age", 11).commit();
在另一个module中获取数据
SharedPreferences sp = getSharedPreferences("sp_demo", Context.MODE_PRIVATE);
String name = sp.getString("name", null);int age = sp.getInt("age", 0);