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;
}
//
}
}
答案 0 :(得分:0)
阅读评论“更改图像是我想要做的。如果我能做到这一点,我不必重新加载视图。条件是布尔值从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