0
A
回答
0
可以創建自定義視圖,並重寫的onDraw(Canvas)的方法和繪製所希望的圖象的位圖。
您可以跟蹤當前度數的「方向」,並使用Canvas.drawBitmap(Bitmap,Matrix,Paint)計算應在哪個位置顯示哪個位置。
下面是根據手機方向移動一個圖像的示例。
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
public class SkySurfaceView extends SurfaceView implements OnTouchListener {
private static final String TAG = SkySurfaceView.class.getSimpleName();
private double viewPortX, viewPortY;
private int starX, starY;
private Bitmap target;
private int targetWidth, targetHeight;
private int vpWidth, vpHeight;
private Matrix skyMatrix, skyMatrix2;
private Matrix skyMatrix3;
private Bitmap star;
private Paint paint;
private Rect touchRect, touchRect2, touchRect3;
public SkySurfaceView(Context context, AttributeSet attr) {
super(context, attr);
viewPortX = viewPortY = 0;
skyMatrix = new Matrix();
skyMatrix2 = new Matrix();
skyMatrix3 = new Matrix();
paint = new Paint();
paint.setStrokeWidth(1);
paint.setDither(true);
paint.setColor(Color.RED);
Options opt = new Options();
opt.inSampleSize = 2;
opt.inScaled = false;
opt.inPreferredConfig = Config.RGB_565;
star = BitmapFactory.decodeResource(getResources(), R.drawable.star,
opt);
touchRect = new Rect(0, 0, star.getWidth(), star.getHeight());
touchRect2 = new Rect(0, 0, star.getWidth(), star.getHeight());
touchRect3 = new Rect(0, 0, star.getWidth(), star.getHeight());
this.setWillNotDraw(true);
setLayerType(View.LAYER_TYPE_HARDWARE, null);
this.setOnTouchListener(this);
getHolder().setFormat(PixelFormat.RGB_565);
}
public void setTarget(Bitmap b) {
target = b;
targetHeight = target.getHeight();
targetWidth = target.getWidth();
}
public void init() {
this.starX = (int) (vpWidth * Math.random()) + vpWidth;
this.starY = (int) ((vpHeight - star.getHeight()) * Math.random());
Log.i(TAG, "drawn star on " + starX + "," + starY);
Canvas c = new Canvas();
Log.i(TAG,
"target dimension is " + target.getWidth() + "x"
+ target.getHeight());
Bitmap bitmap = Bitmap.createBitmap(target.getWidth(),
target.getHeight(), Config.RGB_565);
c.setBitmap(bitmap);
c.drawBitmap(target, 0, 0, paint);
c.drawBitmap(star, starX, starY, paint);
c.drawBitmap(star, starX - targetWidth, starY, paint);
target.recycle();
setTarget(bitmap);
setWillNotDraw(false);
}
@Override
public boolean performClick() {
super.performClick();
return false;
}
/**
*
* @param x
* - [-1:1]
* @param y
* - [-1:1]
*/
public void setViewPort(double x, double y) {
viewPortX = x;
viewPortY = y;
int tempX = (int) (targetWidth * (viewPortX));
int tempY = (int) (targetHeight * (viewPortY - 1));
tempY = Math.max(tempY, -(targetHeight - vpHeight)/2);
tempY = Math.min(tempY, +(targetHeight - vpHeight/2));
Log.i(TAG,
String.format("%d %d , %d %d, %d %d", tempX, tempY, tempX
- targetWidth, tempY, tempX + targetWidth, tempY));
skyMatrix.reset();
skyMatrix.postTranslate(tempX, tempY);
skyMatrix2.reset();
skyMatrix2.postTranslate(tempX - targetWidth, tempY);
skyMatrix3.reset();
skyMatrix3.postTranslate(tempX + targetWidth, tempY);
int xx = (tempX + starX);
while (xx < targetWidth) {
xx += targetWidth;
}
touchRect.offsetTo(xx % targetWidth, (tempY + starY) % targetHeight);
touchRect2.offsetTo(xx % targetWidth - targetWidth, (tempY + starY)
% targetHeight);
touchRect3.offsetTo(xx % targetWidth + targetWidth, (tempY + starY)
% targetHeight);
postInvalidate();
}
public void setViewportSize(int x, int y) {
vpWidth = x;
vpHeight = y;
}
@Override
protected void onDraw(Canvas c) {
super.onDraw(c);
c.drawBitmap(target, skyMatrix, paint);
c.drawBitmap(target, skyMatrix2, paint);
c.drawBitmap(target, skyMatrix3, paint);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
float ex = event.getX();
float ey = event.getY();
Log.i(TAG, "touched " + ex + " " + ey);
if (touchRect.contains((int) ex, (int) ey)) {
if (listener != null) {
listener.onCaptured();
}
} else if (touchRect2.contains((int) ex, (int) ey)) {
if (listener != null) {
listener.onCaptured();
}
} else if (touchRect3.contains((int) ex, (int) ey)) {
if (listener != null) {
listener.onCaptured();
}
}
return false;
}
public void setListener(OnStarCaptureListener listener) {
this.listener = listener;
}
}
在這個例子中,「target」是一個天空圖像。每當setViewPort被調用時,(x和y [-1:1]),圖像的繪圖矩陣被更新,並且調用「invalidate()」。 onDraw()方法調用將使用偏移量繪製「目標」。
相關問題
- 1. Android圖像動畫
- 2. Android:動畫圖像?
- 3. android中的圖像動畫
- 4. 啓動畫面的Android圖像動畫
- 5. 圖像動畫像
- 6. Android平滑圖像動畫
- 7. 動畫隱藏圖像android
- 8. Android翻轉圖像動畫
- 9. 使用CAAnimation動畫圖像
- 10. 使用ImageView動畫圖像
- 11. 動畫圖像
- 12. 動畫圖像
- 13. 圖像動畫和滑動以更改android中的圖像?
- 14. 從多圖像(動畫)GIF文件顯示單個圖像
- 15. 同時動畫多個圖像
- 16. HTML5逐幀動畫(多個圖像)
- 17. kineticJS動畫多個圖像對象
- 18. Openseadragon圖像拖動多個畫布
- 19. 在android中動畫位圖圖像
- 20. Android動畫垂直圖像視圖
- 21. 使用包含多個圖像的圖像中的圖像!
- 22. android多選使用圖像
- 23. Android圖像滑動圖像
- 24. 動畫的多個圖像使用循環C#
- 25. Android OutOfMemoryError使用多個圖像時
- 26. Android圖像移動動畫問題
- 27. 使用CSS3動畫移動圖像
- 28. 動畫圖像wpf
- 29. 圖像不動畫
- 30. J2me動畫圖像
我正在研究那是否有任何它的例子? – 2014-12-05 17:42:12