开发者

screen reload based on condition - android

开发者 https://www.devze.com 2023-03-01 16:19 出处:网络
package com.Example.Company; public class MultipleViewsLayered extends GraphicsActivity { /** Called when the activity is first created. */
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", ++myC开发者_如何学Pythonounter);        
        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;
            }       
        //
    }
}


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

0

精彩评论

暂无评论...
验证码 换一张
取 消