【发布时间】: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 编辑,但不清楚编辑内容是什么?没关系,明白了...点击已编辑旁边的链接。