如何使自定义视图从活动中无效

发布于 2024-12-02 03:12:02 字数 3177 浏览 1 评论 0原文

我有一个应用程序,我想在位图上放置 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

森罗 2024-12-09 03:12:02

为什么半径和关联的 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 several TouchViews in your app at once, all of them would always have the same radius.

Start by removing static from those three things so that each TouchView has its own radius. Next, instead of invoking your TouchView methods from the TouchView class itself, invoke them from your actual TouchView instance which you named touchView above. (e.g. touchView.setRadius(...) instead of TouchView.setRadius(...).)

invalidate is not a static method of a View so you cannot invoke it as one. Views should invalidate themselves when their properties change that would require them to redraw. Since radius is one such property for your TouchView it should call invalidate() on itself at the end of the setRadius method. This will let you remove any direct invalidate calls from your Activity where they don't belong.

极致的悲 2024-12-09 03:12:02

您使用的是类的函数而不是对象,这意味着您使用 :

TouchView.function()

而不是

touchView.function()

invalidate 不是静态函数,因此您需要直接在对象本身上调用它。如果您从非 UI 线程更新视图,请不要忘记使用 postInvalidate()。

You are using the class's function and not the object, meaning you use :

TouchView.function()

instead of

touchView.function()

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文