【问题标题】:screen reload based on condition - android根据条件重新加载屏幕 - android
【发布时间】:2011-08-15 04:01:39
【问题描述】:
package com.Example.Company;

public class MultipleViewsLayered extends GraphicsActivity {
    /** Called when the activity is first created. */

    public int myCounter;
    public boolean oneDone;
    public boolean twoDone;
    public boolean threeDone;
    public boolean fourDone;
    public boolean ScreenCompleted; 
    public ImageView iv3;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.MultipleViewsLayered);

        // Grabbing the Application context         
        final Context context = getApplication();                   
        // Creating a new LinearLayout  add the linear definition again.       
        RelativeLayout relativeLayout = new RelativeLayout(this);                   
        // Setting the orientation to vertical         
        ////relativeLayout.setOrientation(LinearLayout.VERTICAL);                   

        // Creating Fish

        final ImageView iv = new ImageView(this);         
        iv.setImageResource(R.drawable.fish2);
        // relative layout parameters
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(     
                RelativeLayout.LayoutParams.FILL_PARENT, 
                RelativeLayout.LayoutParams.FILL_PARENT);        
        //iv.setId(1);                          
        relativeLayout.addView(iv,lp);        

        // Creating transparent image with numbers.
        final ImageView iv2 = new ImageView(this);
        iv2.setImageResource(R.drawable.ctdsquareone);
        //iv2.setId(2);
        RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(     
                RelativeLayout.LayoutParams.FILL_PARENT, 
                RelativeLayout.LayoutParams.FILL_PARENT);
        relativeLayout.addView(iv2,lp2);                    

        final CustomViewCanvas myCanvas = new CustomViewCanvas(this);
        RelativeLayout.LayoutParams lp3 = new RelativeLayout.LayoutParams(     
                RelativeLayout.LayoutParams.FILL_PARENT, 
                RelativeLayout.LayoutParams.FILL_PARENT);
        relativeLayout.addView(myCanvas,lp3);

        setContentView(relativeLayout);              

     // Get the app's shared preferences        
        SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor editor = app_preferences.edit();       
        myCounter = app_preferences.getInt("myCounter", 0);     
        // Increment the counter                       
        editor.putInt("myCounter", ++myCounter);        
        editor.commit(); 

        oneDone = false;
        twoDone = false;
        threeDone = false;
        fourDone = false;
        ScreenCompleted = false;

        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setColor(0xFFFF0000);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(12);       

        mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 },
                                       0.4f, 6, 3.5f);

        mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL);    

    } 

    private Paint       mPaint;
    private MaskFilter  mEmboss;
    private MaskFilter  mBlur;


    public class CustomViewCanvas extends View {
        Paint paint = new Paint();      

        private Bitmap  mBitmap;
        private Paint   mBitmapPaint;

        private Path mPath;
        private Canvas  mCanvas;


        public CustomViewCanvas (Context context){
            super(context);
            paint.setColor(Color.BLACK);

            //New Bitmap empty
            mBitmap = Bitmap.createBitmap(480, 800, Bitmap.Config.ARGB_8888); 
            mCanvas = new Canvas(mBitmap);           
            mBitmapPaint = new Paint(Paint.DITHER_FLAG);

            //Path
            mPath = new Path();                                                 

        }        

        @Override
        protected void onDraw(Canvas canvas) {
            canvas.drawColor(Color.TRANSPARENT);                        
            canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);


            //Path
            mCanvas.drawPath(mPath, mPaint);            

        }

        //
         private float mX, mY;
            private static final float TOUCH_TOLERANCE = 4;

            private void touch_start(float x, float y) {
                mPath.reset();
                mPath.moveTo(x, y);
                mX = x;
                mY = y;
            }
            private void touch_move(float x, float y) {
                float dx = Math.abs(x - mX);
                float dy = Math.abs(y - mY);
                if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
                    mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
                    mX = x;
                    mY = y;

            //*************************************************************************

            // HERE IS THE ISSUE
            // I WANT TO CHANGE the IV IMAGE HERE WHEN ScreenCompleted IS TRUE.
            // IT WILL BE SET TO TRUE HERE WHEN A LINE IS COMPLETED.

            //*************************************************************************


                }              
            }

            private void touch_up() {
                mPath.lineTo(mX, mY);
                // commit the path to our offscreen            
                mCanvas.drawPath(mPath, mPaint);                
                // kill this so we don't double draw
                mPath.reset();
            }

            @Override
            public boolean onTouchEvent(MotionEvent event) {
                float x = event.getX();
                float y = event.getY();

                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        touch_start(x, y);
                        invalidate();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        touch_move(x, y);
                        invalidate();
                        break;
                    case MotionEvent.ACTION_UP:
                        touch_up();
                        invalidate();
                        break;
                }
                return true;
            }       
        //
    }
}

【问题讨论】:

  • 有问题吗?
  • 哦,原来是隐藏在代码里的。非常微妙。
  • 条件是什么?为什么要重绘整个屏幕而不只是更改图像?
  • 更改图像是我想要做的。如果我能做到这一点,我就不必重新加载视图。条件是一个从真到假的布尔值。
  • MByD 的编辑究竟是什么?上面写着 6 分钟前由 MByD 编辑,但不清楚编辑内容是什么?没关系,明白了...点击已编辑旁边的链接。

标签: android screen reload


【解决方案1】:

更新:

阅读评论“更改图像是我正在尝试做的事情。如果我能做到,我不必重新加载视图。条件是一个布尔值,从真到假",我想您需要的只是一种从其他类引用小部件的方法。然后,只需使用新的setImageResource (RID); 更改图像就足以用新图像刷新屏幕。这是一个完整的例子。有一个类在单击按钮时检查是否选中了复选框。如果是,那么它将改变图像:

ListViewTest.java

package com.aleadam.test;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class ListViewTest extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout ll = new LinearLayout(this);
        ImageView img = new ImageView(this);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        img.setImageResource(R.drawable.image1);
        MyLayout myl = new MyLayout (this);
        myl.setImage(img);
        ll.addView(img, lp);
        ll.addView(myl, lp);
        setContentView (ll);
    }
}

MyLayout.java

package com.aleadam.test;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MyLayout extends LinearLayout {
    private ImageView img;
    private Button btn;
    private CheckBox chkbox;
    public MyLayout(Context context) {
        super (context);
        this.setOrientation(VERTICAL);
        btn = new Button (context);
        chkbox = new CheckBox (context);
        btn.setText("Click");
        btn.setOnClickListener(new MyListener());
        chkbox.setText("Select");
        LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        this.addView(btn, lp);
        this.addView(chkbox,lp);
    }
    public void setImage (ImageView view) {
        this.img = view;
    }
    private class MyListener implements OnClickListener {
        public void onClick (View v) {
            if (chkbox.isChecked()) {
                img.setImageResource(R.drawable.image2);
            }
        }
    }
}

如果您的 Class2 对象在屏幕上可见,请尝试 this.getRootView().invalidate();

从查看参考页面:

绘图

通过遍历树并渲染与无效区域相交的每个视图来处理绘图。因为树是按顺序遍历的,这意味着父母将在他们的孩子之前(即之后)绘制,兄弟姐妹按照它们在树中出现的顺序绘制。如果您为 View 设置了背景可绘制对象,则 View 会在回调其 onDraw() 方法之前为您绘制它。

请注意,框架不会绘制不在无效区域中的视图。

要强制绘制视图,请调用 invalidate()。

http://developer.android.com/reference/android/view/View.html

【讨论】:

  • Aleadam,我已经通过文档多次尝试找到解决方案。使用 this.getRootView().invalidate() 使父视图无效是我的第一次尝试。它不起作用。
  • 这个commnets 分为两部分,太长了... Aleadam,我发现我们的代码有一个不同。对于第二个对象,我执行 Class2 扩展 View,您执行 Class MyLayout 扩展 LinearLayout。在我的课堂上,如果我这样做了 this.addview 我会得到一个错误。我是否正确,因为我在视图中而不是布局中,所以我不能添加项目?这是错误吗?如何引用 Class2 的相对布局,或者只是因为我无法扩展视图?我希望我不会让您对我的 cmets 更加困惑。
  • 同时,我将测试是否可以更改我的 Class2 以扩展布局,在其中添加我的视图(画布),添加另一个按钮,这样我就可以有一个监听器,当单击按钮时,图像 iv会改变。对于按钮,我将尝试在布尔值更改并最终使其不可见时调用 onclick 事件。
  • 只要我再获得 2 个声望,我就会投票给你。顺便说一句,我很感激你停下来提出问题。我真的很喜欢这里。
  • @George 是的,addView(...) 不是来自View 类的方法,而是来自ViewGroup,其中LinearLayout 是一个子类。如果您要向您的类添加多个小部件,那么扩展任何ViewGroup 子类更有意义(我只是选择LinearLayout 以使其简单)。您现在应该可以开始上课了。祝你好运!
猜你喜欢
  • 1970-01-01
  • 2012-05-25
  • 1970-01-01
  • 2012-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-25
相关资源
最近更新 更多