Android的自定义视图与处理程序 [英] Android Custom View with Handler
问题描述
我要寻找一个简单的方法在Android的动画自定义视图。我试图避免使用动画对象,但要与原纱一起工作。我已经通过创建一类来完成创建一个自定义视图扩展 android.view.View。然后,我重写的onDraw 方式和使用画布绘制一个矩形。我想是缩小矩形,所以我保持这种重新presents矩形的右侧的x值的变量。然后我会像矩形的右边缘收缩在一段时间。我也喜欢做的就是创建一个新的线程,启动它,并让它改变矩形的值。 ,除了该视图的所有作品没有得到更新,直到你调用 View.invalidate 。问题是,因为它不是UI线程我不能调用从我催生了线程。我使用的处理程序阅读解决方案...但我仍然不能确定,如果这是正确的解决方案,以及如何使用它们。
包com.example.practicum;进口android.content.Context;
进口android.graphics.Canvas;
进口android.graphics.Color;
进口android.graphics.Paint;
进口android.graphics.Rect;
进口android.os.Handler;
进口android.os.Message;
进口android.util.AttributeSet;
进口android.view.View;公共类TimerControl扩展视图实现Runnable,Handler.Callback
{
私人涂料粉刷;
私人矩形RECT;
私人线程t;
私人处理程序H; 公共TimerControl(上下文的背景下,ATTRS的AttributeSet)
{
超(背景下,ATTRS);
// TODO自动生成构造函数存根 油漆=新的油漆();
paint.setColor(Color.BLUE);
RECT =新的矩形(0,0,60,60);
T =新的Thread(本);
t.start();
H =新的处理程序(本);
//h.post(this);
} @覆盖
公共无效的onDraw(帆布油画)
{
canvas.drawRect(RECT,油漆);
} @覆盖
公共无效的run()
{
rect.right = rect.right-1;
而(真)
{
rect.right = rect.right-1;
this.invalidate();
尝试
{
视频下载(5000);
h.sendEmptyMessage(0);
}
赶上(InterruptedException的E)
{
// TODO自动生成catch块
e.printStackTrace();
}
}
} @覆盖
公共布尔的handleMessage(消息MSG)
{ 返回false;
}}
您可以定义一个全局处理程序(在UI线程):
处理程序mHandler =新的处理程序();
那么,从你的线程,请致电:
mHandler.post(新的Runnable(){
公共无效的run(){
//调用UI线程上的一些方法
}
});
I am looking for an easy way to animate custom views in Android. I am trying to avoid using the animator object but want to work with raw threads. What I have done is created a custom view by creating a class that extends android.view.View. I then override the onDraw method and use the canvas to draw a rect. What I would like is the rect to shrink, so I keep a variable that represents the x value of the right hand side of the rectangle. I would then like the the rectangle's right edge shrink in over time. What I would have liked to do is create a new thread, start it and have it change the value of the rectangle. That all works except the view doesn't get updated until you call View.invalidate. The problem is that I can't call that from the thread that I spawned because it is not the UI thread. I read solutions on using Handlers... but I am still unsure if that is the correct solution and then how to use them.
package com.example.practicum;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;
public class TimerControl extends View implements Runnable, Handler.Callback
{
private Paint paint;
private Rect rect;
private Thread t;
private Handler h;
public TimerControl(Context context, AttributeSet attrs)
{
super(context, attrs);
// TODO Auto-generated constructor stub
paint = new Paint();
paint.setColor(Color.BLUE);
rect = new Rect(0,0,60,60);
t = new Thread(this);
t.start();
h = new Handler(this);
//h.post(this);
}
@Override
public void onDraw(Canvas canvas)
{
canvas.drawRect(rect, paint);
}
@Override
public void run()
{
rect.right = rect.right-1;
while(true)
{
rect.right = rect.right-1;
this.invalidate();
try
{
Thread.sleep(5000);
h.sendEmptyMessage(0);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public boolean handleMessage(Message msg)
{
return false;
}
}
you can define a global Handler (in the UI thread):
Handler mHandler = new Handler();
then, from your thread, call:
mHandler.post(new Runnable() {
public void run() {
// call some method on the UI thread
}
});
这篇关于Android的自定义视图与处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!