根据条件重新加载屏幕 - android
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更新:
阅读评论后“更改图像是我想要做的。如果我能做到这一点,我就不必重新加载视图。条件是从 true 到 false 的布尔值 ”,我想您需要的只是一种从其他类引用小部件的方法。然后,只需使用新的
setImageResource (RID);
更改图像就足以使用新图像刷新屏幕。这是一个完整的例子。有一个类在单击按钮时检查复选框是否被选中。如果是,那么它将更改图像:ListViewTest.java
MyLayout.java
如果您的 Class2 对象在屏幕上可见,请尝试
this.getRootView() .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
MyLayout.java
If your Class2 object is visible on the screen, try
this.getRootView().invalidate();
From the View reference page:
http://developer.android.com/reference/android/view/View.html