如何使自定义视图从活动中无效
我有一个应用程序,我想在位图上放置 2 个圆圈。我有 2 个按钮,一个用于增加半径,一个用于减小半径。这些按钮是根据活动中的 xml 进行扩充的。活动的视图是自定义视图。我可以让按钮显示在视图上,并且两者都相应地改变半径变量。到目前为止,一切都很好。单击任一按钮时我无法使视图无效。应该发生的是,当单击按钮时,半径发生变化,然后重新绘制画布以显示半径变化。如何从 onclick() 调用 invalidate?我不确定这是否是最好的方法。谢谢。 。
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Jjilapp extends Activity {
private static final String TAG = "*********jjil";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.touchview);
final TouchView touchView = (TouchView)findViewById(R.id.touchview);
final HorizontalSlider slider = (HorizontalSlider)findViewById(R.id.slider);
touchView.initSlider(slider);
Button plus = (Button)findViewById(R.id.plus);
plus.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Log.e(TAG, "onClickplus");
TouchView.setRadius(TouchView.getRadius() + 5);
Log.e(TAG, "radius = "+TouchView.getRadius());
}}) ;
Button minus = (Button)findViewById(R.id.minus);
minus.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
TouchView.setRadius(TouchView.getRadius() - 5);
Log.e(TAG, "radius = "+TouchView.getRadius());
TouchView.invalidate();// DOESN'T WORK***************
}}) ;
}//end of oncreate
}//end of jjilapp
。
public class TouchView extends View{
private static int radius = 50;
public TouchView(Context context) {
super(context);
}
public TouchView(Context context, AttributeSet attr) {
super(context,attr);
Log.e(TAG, "++++++++++ inside touchview constructor");
pTouch = new Paint(Paint.ANTI_ALIAS_FLAG);
pTouch.setXfermode(new PorterDuffXfermode(Mode.SRC_OUT));
pTouch.setColor(Color.TRANSPARENT);
pTouch.setStyle(Paint.Style.STROKE);
}// end of touchView constructor
@Override
public void onDraw(Canvas canvas){
super.onDraw(canvas);
Log.e(TAG, "******about to draw bgr ");
canvas.drawBitmap(bgr, 0, 0, null);
canvas.drawCircle(centreX, centreY, radius,pTouch);
canvas.drawCircle(centreA, centreB, radius,pTouch);
}
}//end of onDraw
public static int getRadius() {
return radius;
}
public static void setRadius(int r) {
radius = r;
}
}
I've an app where i'm wanting to place 2 circles on a bitmap. i've got 2 buttons, one for increasing the radius and one for decreasing the radius. The buttons are inflated from xml in the activity. the activity's view is a custom view. i can get the buttons to show on the view and the both alter the radius variable accordingly. so far so good. i can't get the view to invalidate when either button is clicked. what should happen, is when the button is clicked the radius is changed then the canvas redrawn to show the radius changes. How can i call invalidate from the onclick()? I'm not sure if this is the best way of doing this. thanks.
.
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Jjilapp extends Activity {
private static final String TAG = "*********jjil";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.touchview);
final TouchView touchView = (TouchView)findViewById(R.id.touchview);
final HorizontalSlider slider = (HorizontalSlider)findViewById(R.id.slider);
touchView.initSlider(slider);
Button plus = (Button)findViewById(R.id.plus);
plus.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Log.e(TAG, "onClickplus");
TouchView.setRadius(TouchView.getRadius() + 5);
Log.e(TAG, "radius = "+TouchView.getRadius());
}}) ;
Button minus = (Button)findViewById(R.id.minus);
minus.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
TouchView.setRadius(TouchView.getRadius() - 5);
Log.e(TAG, "radius = "+TouchView.getRadius());
TouchView.invalidate();// DOESN'T WORK***************
}}) ;
}//end of oncreate
}//end of jjilapp
.
public class TouchView extends View{
private static int radius = 50;
public TouchView(Context context) {
super(context);
}
public TouchView(Context context, AttributeSet attr) {
super(context,attr);
Log.e(TAG, "++++++++++ inside touchview constructor");
pTouch = new Paint(Paint.ANTI_ALIAS_FLAG);
pTouch.setXfermode(new PorterDuffXfermode(Mode.SRC_OUT));
pTouch.setColor(Color.TRANSPARENT);
pTouch.setStyle(Paint.Style.STROKE);
}// end of touchView constructor
@Override
public void onDraw(Canvas canvas){
super.onDraw(canvas);
Log.e(TAG, "******about to draw bgr ");
canvas.drawBitmap(bgr, 0, 0, null);
canvas.drawCircle(centreX, centreY, radius,pTouch);
canvas.drawCircle(centreA, centreB, radius,pTouch);
}
}//end of onDraw
public static int getRadius() {
return radius;
}
public static void setRadius(int r) {
radius = r;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么半径和关联的 get/set 方法是静态的?这意味着即使您的应用中同时有多个 TouchView,它们也始终具有相同的半径。
首先从这三件事中删除
static
,以便每个TouchView
都有自己的半径。接下来,不要从TouchView
类本身调用TouchView
方法,而是从您命名为touchView< 的实际
TouchView
实例调用它们。 /代码> 上面。 (例如touchView.setRadius(...)
而不是TouchView.setRadius(...)
。)invalidate
不是以下方法的静态方法一个视图,因此您不能将其作为一个视图来调用。当视图的属性发生变化而需要重绘时,视图本身应该失效
。由于 radius 是TouchView
的此类属性之一,因此它应该在setRadius
方法末尾调用自身的invalidate()
。这将使您可以从 Activity 中删除任何不属于它们的直接invalidate
调用。Why are
radius
and the associated get/set methods static? That means that even if you had severalTouchViews
in your app at once, all of them would always have the same radius.Start by removing
static
from those three things so that eachTouchView
has its own radius. Next, instead of invoking yourTouchView
methods from theTouchView
class itself, invoke them from your actualTouchView
instance which you namedtouchView
above. (e.g.touchView.setRadius(...)
instead ofTouchView.setRadius(...)
.)invalidate
is not a static method of a View so you cannot invoke it as one. Views shouldinvalidate
themselves when their properties change that would require them to redraw. Since radius is one such property for yourTouchView
it should callinvalidate()
on itself at the end of thesetRadius
method. This will let you remove any directinvalidate
calls from your Activity where they don't belong.您使用的是类的函数而不是对象,这意味着您使用 :
而不是
invalidate 不是静态函数,因此您需要直接在对象本身上调用它。如果您从非 UI 线程更新视图,请不要忘记使用 postInvalidate()。
You are using the class's function and not the object, meaning you use :
instead of
invalidate is not a static function, so you need to call it directly on the object itself. Don't forget to use postInvalidate() in case you are updating the view from a non-UI thread.