根据条件重新加载屏幕 - android

发布于 2024-11-03 16:25:06 字数 6524 浏览 0 评论 0原文

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;
            }       
        //
    }
}
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;
            }       
        //
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

廻憶裏菂餘溫 2024-11-10 16:25:06

更新:

阅读评论后“更改图像是我想要做的。如果我能做到这一点,我就不必重新加载视图。条件是从 true 到 false 的布尔值 ”,我想您需要的只是一种从其他类引用小部件的方法。然后,只需使用新的 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

Update:

after reading the comment "Changing the image is what I am trying to do. If I can do that I don't have to reload the view. The condition is a boolean turning from true to false", I guess what you need is just a way to reference a widget from other class. Then, just changing the image by using a new setImageResource (RID); should be enough to refresh the screen with the new image. Here is a complete example of it. There's a class that upon clicking on a button, checks whether a checkbox is checked or not. If it is, then it will change the image:

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);
            }
        }
    }
}

If your Class2 object is visible on the screen, try this.getRootView().invalidate();

From the View reference page:

Drawing

Drawing is handled by walking the tree and rendering each view that intersects the the invalid region. Because the tree is traversed in-order, this means that parents will draw before (i.e., behind) their children, with siblings drawn in the order they appear in the tree. If you set a background drawable for a View, then the View will draw it for you before calling back to its onDraw() method.

Note that the framework will not draw views that are not in the invalid region.

To force a view to draw, call invalidate().

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

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