7
A
回答
-1
Matrix mat = new Matrix();
mat.preRotate(angle);///in degree
Bitmap mBitmap = Bitmap.createBitmap(originalBmp, 0, 0, modWidth, modHeight, mat, true);
//originalBmp -> original img as bitmap
//modHeight -> new height
//modWidth -> new width
使用上面的代碼。
+0
你的答案不是這個問題的解決方案。它只是指導如何使用矩陣旋轉圖像。 – secretlm 2012-04-22 08:47:43
4
這裏是基本代碼加載一個位圖和旋轉向左或向右:
// Load a bitmap from a drawable, make sure this drawable exists in your project
Bitmap sprite = BitmapFactory.decodeResource(this.getResources(),
R.drawable.ic_launcher);
// Create two matrices that will be used to rotate the bitmap
Matrix rotateRight = new Matrix();
Matrix rotateLeft = new Matrix();
// Set the matrices with the desired rotation 90 or -90 degrees
rotateRight.preRotate(90);
rotateLeft.preRotate(-90);
// Create bitmaps based on the loaded bitmap 'sprite' and apply one of
// the rotation matrices
Bitmap rSprite = Bitmap.createBitmap(sprite, 0, 0,
sprite.getWidth(), sprite.getHeight(), rotateRight, true);
Bitmap lSprite = Bitmap.createBitmap(sprite, 0, 0,
sprite.getWidth(), sprite.getHeight(), rotateLeft, true);
現在去,並用rSprite和lSprite。
這裏是實際繪製位圖到屏幕全樣本:
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new drawView(this));
}
private class drawView extends View{
public drawView(Context context){
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// Load a bitmap from a drawable, make sure this drawable exists in your project
Bitmap sprite = BitmapFactory.decodeResource(this.getResources(),
R.drawable.ic_launcher);
// Create two matrices that will be used to rotate the bitmap
Matrix rotateRight = new Matrix();
Matrix rotateLeft = new Matrix();
// Set the matrices with the desired rotation 90 or -90 degrees
rotateRight.preRotate(90);
rotateLeft.preRotate(-90);
// Create bitmaps based on the loaded bitmap 'sprite' and apply one of
// the rotation matrices
Bitmap rSprite = Bitmap.createBitmap(sprite, 0, 0,
sprite.getWidth(), sprite.getHeight(), rotateRight, true);
Bitmap lSprite = Bitmap.createBitmap(sprite, 0, 0,
sprite.getWidth(), sprite.getHeight(), rotateLeft, true);
//Draw the first unrotated sprite at the top left of the screen
canvas.drawBitmap(sprite, 0, 0, null);
//Draw the rotated right sprite on the 2nd row
canvas.drawBitmap(rSprite, 0, sprite.getHeight() + 5, null);
//Draw the rotated left sprite on the 3rd row
canvas.drawBitmap(lSprite, 0, sprite.getHeight() * 2 + 5, null);
}
}
}
6
使用下面
public class Rotateimage extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery);
// or just load a resource from the res/drawable directory:
Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.flo);
// find the width and height of the screen:
Display d = getWindowManager().getDefaultDisplay();
int x = d.getWidth();
int y = d.getHeight();
// get a reference to the ImageView component that will display the image:
ImageView img1 = (ImageView)findViewById(R.id.imageView1);
// scale it to fit the screen, x and y swapped because my image is wider than it is tall
Bitmap scaledBitmap = Bitmap.createScaledBitmap(myBitmap, y, x, true);
// create a matrix object
Matrix matrix = new Matrix();
matrix.postRotate(45, 90, 180);
// create a new bitmap from the original using the matrix to transform the result
Bitmap rotatedBitmap = Bitmap.createBitmap(scaledBitmap , 0, 0, scaledBitmap .getWidth(), scaledBitmap .getHeight(), matrix, true);
// display the rotated bitmap
img1.setImageBitmap(rotatedBitmap);
}}
0
使用此代碼希望這將是有幫助的代碼... 你必須有寫這個方法做旋轉運算
public void paintFromCenter(float angle, Canvas c) {
Bitmap b = sprite;
Bitmap h = b;
Matrix matrix = new Matrix();
matrix.postRotate(angle, h.getWidth()/2, h.getHeight());
matrix.postTranslate(getX(), getY());
// canvas.drawBitmap(bitmap, matrix, new Paint());
Bitmap bmp2 = Bitmap.createBitmap(h, 0, 0, frameWidth, frameHeight,
matrix, true);
c.drawBitmap(h, matrix, null);
}
在你的程序中你必須寫onTo uchEvent()方法
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
edX = (int) event.getX();
edY = (int) event.getY();
if ((edX > objectSprite.getX()
&& edX < objectSprite.getX() + objectSprite.getWidth()
&& edY > objectSprite.getY() && edY < objectSprite.getY()
+ objectSprite.getHeight())) {
}
}
if (action == MotionEvent.ACTION_MOVE) {
emX = (int) event.getX();
emY = (int) event.getY();
if (edX < emX && angle <= 90) {
update(canvas);
CHAR = 'D';
} else if (edX > emX && angle > 0) {
update(canvas);
CHAR = 'U';
}
if (edY < emY && angle <= 90) {
update(canvas);
CHAR = 'L';
} else if (edY > emY && angle >= 0) {
update(canvas);
CHAR = 'R';
}
}
return true;
}
,你必須創建update()方法來旋轉你的觸摸動作角角度可以定義爲您的選擇。起初,我有申報
int angle=1;
public void update(Canvas canvas) {
switch (CHAR) {
case 'D':
angle += 1;
break;
case 'U':
angle -= 1;
break;
case 'R':
angle -= 1;
break;
case 'L':
angle += 1;
break;
}
}
現在你要畫在畫你的對象()方法,這樣
public void draw() {
objectSprite.paintFromCenter(angle, canvas);
}
0
你只需要添加這個簡單的代碼到您的按鈕
imVCature_pic.setRotation(imVCature_pic.getRotation() + 90);
相關問題
- 1. Android ||位圖圖像旋轉
- 2. 如何在Android中旋轉圖像?
- 3. 連續旋轉圖像OnTouchEvent在Android中
- 4. Android的圖像旋轉
- 5. Android大圖像旋轉
- 6. 的Android - 旋轉圖像
- 7. Android,幫助旋轉圖像
- 8. android - 圖片旋轉在圖像按鈕
- 9. 在javascript中旋轉圖像
- 10. 在Symfony2中旋轉圖像
- 11. 在OpenCV中旋轉圖像
- 12. 在UIImageView中旋轉圖像
- 13. 在RemoteViews中旋轉圖像?
- 14. 在Java中旋轉圖像
- 15. 在圖像中旋轉圖像
- 16. 點在旋轉圖像上拖曳 - android
- 17. Android - 圍繞中心點旋轉圖像?
- 18. - 旋轉圖像
- 19. 旋轉圖像
- 20. 旋轉圖像
- 21. 旋轉圖像
- 22. 旋轉圖像
- 23. Android攝像頭在旋轉
- 24. 在jquery中旋轉圖像並保存在jquery中的旋轉圖像
- 25. Android - 如何在圖像上旋轉圖像查看
- 26. matrix.postRotate(90)在圖像旋轉
- 27. 圖像在角落旋轉
- 28. Magento中的圖像旋轉
- 29. ImageView中的旋轉圖像
- 30. 旋轉表中的圖像
看到這裏回答:http://stackoverflow.com/questions/13099652/custom-rotating-of-an-image-view – AYBABTU 2015-05-04 14:55:15