2013-04-03 207 views
1

您好,我有兩個圖像,我合併,使用帆布拉伸圖像用帆布的Android

這裏的圖像是下面的代碼

 Intent intent = getIntent(); 

     //bm is the image get from camera 
     bm = BitmapFactory.decodeFile(intent.getStringExtra("getdata")); 

     //this is simple white text image 
     bm1 = BitmapFactory.decodeResource(getResources(), R.drawable.txt_made_hawk_nelson); 
     imgSeletedPhoto = (ImageView) findViewById(R.id.imgSelectedPhoto); 

     int width = bm.getWidth(); 
     int height = bm.getHeight(); 

     int maxWidth = (width > bm1.getWidth() ? width : bm1.getWidth()); 
     int maxHeight = (height > bm1.getHeight() ? height : bm1.getHeight()); 


     // calculate the scale - in this case = 0.4f 
     float scaleWidth = ((float) maxWidth)/width; 
     float scaleHeight = ((float) maxHeight)/height; 

     Matrix matrix = new Matrix(); 
     matrix.postScale(scaleWidth, scaleHeight);   
    bmOverlay = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true); 

    Canvas canvas = new Canvas(bmOverlay); 
    //canvas.drawBitmap(bm, 0, 0, null); 
    canvas.drawBitmap(bm1, 0, 50, null); 
    imgSeletedPhoto.setImageBitmap(bmOverlay); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

我要縮放圖像和圖像看起來應該像下面

enter image description here

而是由上面的代碼做它看起來像

此文件的

enter image description here

XML代碼

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" 
android:weightSum="10" > 

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:layout_weight="7" > // this is covering 70% of screen 

    <ImageView 
     android:id="@+id/imgSelectedPhoto" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_centerInParent="true" /> 
</RelativeLayout> 

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:layout_weight="3" 
    android:background="#baca9d" 
    android:gravity="center" 
    android:orientation="vertical" 
    android:paddingLeft="10dp" 
    android:paddingRight="10dp" > 

任何機構可以幫助我如何拉伸圖像,使之從​​底部和頂部側面也充分把文本中心

+0

你真正想要的位圖高度是多少? – Triode

+0

我正在編輯我的問題(位圖大小加載屏幕大小的70%的面積 –

回答

5
int targetWidth = 70% of screen width; 
int targetHeight = 70% of screen height; 

int saclex = (int)((float)targetWidth/(float)bmp1.getWidth()); 
int sacley = (int)((float)targetHeight/(float)bmp1.getHeight()); 

將比例值應用到bmp1並創建縮放比特圖

saclex = (int)((float)targetWidth/(float)bmp.getWidth()); 
sacley = (int)((float)targetHeight/(float)bmp.getHeight()); 

應用比例值bmp並創建縮放位圖。希望這會有所幫助。否則你可以使用這種方法來創建它。

Bitmap bitmap = Bitmap.createScaledBitmap(mTargetImage, bWidth, 
       bHeight, false); 
+0

雅我已經完成了你的建議,它是完美的罰款從頂部和底部,但現在它不覆蓋從寬邊的全部區域:( –

+0

我們可以發佈代碼只有計算部分 – Triode

+0

工作thnks :) –