android自定义控件(六) 刷新
2015-09-09 16:41
447 查看
三种得到LinearInflater的方法
a. LayoutInflater inflater = getLayoutInflater();
b. LayoutInflater localinflater =
(LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
c. LayoutInflater inflater = LayoutInflater.from(context);
onDraw 方法绘图,invalidate刷新界面。
效果图:
点击一下换颜色
onDraw画完图后,给控件设置点击事件 ,将参数传到控件里,然后invalidate刷新
1.onDraw画图,并增加changeColor方法
[java] view
plaincopy
public class CusView3 extends View {
private int color = 0;
public CusView3(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint mPaint = new Paint();
if (color > 2) {
color = 0;
}
switch (color) {
case 0:
mPaint.setColor(Color.GREEN);
break;
case 1:
mPaint.setColor(Color.RED);
break;
case 2:
mPaint.setColor(Color.BLUE);
break;
default:
break;
}
mPaint.setStyle(Style.FILL);
mPaint.setTextSize(35.0f);
canvas.drawText("点击我刷新", 10, 60, mPaint);
}
public void changeColor() { //为了让外面调用
color++;
}
}
2.布局
[html] view
plaincopy
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<xue.test.CusView3
android:id="@+id/cusview3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</xue.test.CusView3>
</LinearLayout>
3.画图后 给控件设置点击事件 ,将参数传到控件里,然后invalidate刷新
[java] view
plaincopy
public class TestCustomViewActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
view3 = (CusView3) findViewById(R.id.cusview3);
// 点击事件
view3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Message message = new Message();
message.what = 1;
myHandler.sendMessage(message);
}
});
}
Handler myHandler = new Handler() {
// 接收到消息后处理
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
// 调用方法
view3.changeColor();
// 刷新方法
view3.invalidate();
break;
}
super.handleMessage(msg);
}
};
private CusView3 view3;
}
至于自定义控件占整屏的问题,可能需要用layoutparams
源码下载: http://download.csdn.net/detail/ethan_xue/4152203
a. LayoutInflater inflater = getLayoutInflater();
b. LayoutInflater localinflater =
(LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
c. LayoutInflater inflater = LayoutInflater.from(context);
onDraw 方法绘图,invalidate刷新界面。
效果图:
点击一下换颜色
onDraw画完图后,给控件设置点击事件 ,将参数传到控件里,然后invalidate刷新
1.onDraw画图,并增加changeColor方法
[java] view
plaincopy
public class CusView3 extends View {
private int color = 0;
public CusView3(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint mPaint = new Paint();
if (color > 2) {
color = 0;
}
switch (color) {
case 0:
mPaint.setColor(Color.GREEN);
break;
case 1:
mPaint.setColor(Color.RED);
break;
case 2:
mPaint.setColor(Color.BLUE);
break;
default:
break;
}
mPaint.setStyle(Style.FILL);
mPaint.setTextSize(35.0f);
canvas.drawText("点击我刷新", 10, 60, mPaint);
}
public void changeColor() { //为了让外面调用
color++;
}
}
2.布局
[html] view
plaincopy
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<xue.test.CusView3
android:id="@+id/cusview3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</xue.test.CusView3>
</LinearLayout>
3.画图后 给控件设置点击事件 ,将参数传到控件里,然后invalidate刷新
[java] view
plaincopy
public class TestCustomViewActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
view3 = (CusView3) findViewById(R.id.cusview3);
// 点击事件
view3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Message message = new Message();
message.what = 1;
myHandler.sendMessage(message);
}
});
}
Handler myHandler = new Handler() {
// 接收到消息后处理
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
// 调用方法
view3.changeColor();
// 刷新方法
view3.invalidate();
break;
}
super.handleMessage(msg);
}
};
private CusView3 view3;
}
至于自定义控件占整屏的问题,可能需要用layoutparams
源码下载: http://download.csdn.net/detail/ethan_xue/4152203
相关文章推荐
- android自定义控件(三) 增加内容 自定义属性 format详解
- android自定义控件(四) View中的方法
- android 5.0 statusbar状态栏透明 解决方案
- Android Handler ExecutorService(线程池) 缓存模式
- 译:Android Configuration(Android 配置)
- Android 补间动画
- Qt生成exe文件上贴图 Qt5.5 on Android 可执行程序贴图
- Android编程中,sp和px和dp有什么区别
- android测试性能工具
- Android xml资源文件中@、@android:type、@*、?、@+含义和区别
- android声音调整源代码分析
- Android手势识别(单击 双击 抬起 短按 长按 滚动 滑动)
- Android中的Service 与 Thread 的区别[转]
- Android基础入门教程——7.3.3 Android 文件下载(2)
- android代码中设置控件位置属性的方法
- Android基础入门教程——7.3.2 Android 文件下载(1)
- android自定义控件(五) 自定义组合控件
- Android基础入门教程——7.3.1 Android 文件上传
- android中TextView实现多个可点击的文本
- Android应用程序进程启动过程的源代码分析