2014-04-16 67 views
-1

我想顯示矩形傾斜到90度的圖像。我怎麼能做到這一點?在矩形中顯示圖像?

enter image description here

在這個框架我想顯示圖像

請給我一些解決方案。

謝謝。

+3

我看到一個正方形。傾斜在45°... –

+0

http://stackoverflow.com/questions/21002224/add-a-background-image-to-shape-in​​-xml-android – weston

+1

@BobMalooga廣場是矩形的子類:) –

回答

0

使用下面的方法。

public static Bitmap rotate(Bitmap src, float degree) { 
    // create new matrix 
    Matrix matrix = new Matrix(); 
    // setup rotation degree 
    matrix.postRotate(degree); 

    // return new bitmap rotated using matrix 
    return Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true); 
} 
1

隨着API> = 11

mImageView.setRotation(angle) 

另一種方法:

ImageView img = (ImageView)findViewById(R.id.yourImageViewId); 
Options o = getSize(this, R.drawable.yourImage); 
Matrix m = new Matrix(); 
m.postRotate(angle, o.outWidth/2, o.outHeight/2); 
img.setScaleType(ScaleType.MATRIX); 
img.setImageMatrix(m); 
+0

+爲簡單起見 –

3

你可以創建一個45D的動畫,並把它應用到你的ImageView的,就像這樣:

ImageView image= (ImageView)findViewById(R.id.imageView); 
    // Create 45d animaion 
    Animation an = new RotateAnimation(0.0f, 45f, image.getPivotX(), image.getPivotY()); 

    // Set the animation's parameters 
    an.setDuration(1);    
    an.setRepeatCount(0);     
    an.setRepeatMode(Animation.REVERSE); 
    an.setFillAfter(true);    

    // Aplly animation to image 
    image.setAnimation(an); 
0

適用於所有API 放置在您的Layout XML中使用ImageView,並在您的Activity的onCreate()中爲其設置動畫:

float rotationDegree = -45; //You can change if you need. 
     Animation anim = new RotateAnimation(0,rotationDegree); 
     anim.setFillAfter(true); 
     imageView.startAnimation(anim);