2011-10-29 18 views
1

写真を選択してImageViewで表示するアプリケーションがあります。 ギャラリーと同じ方法で、選択した写真をギャラリーに表示するようにしました。ImageViewで枠線を作成するには?

CENTER_CROPCENTER_INSIDEを使用しようとしましたが、写真が境界線を越えています。

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode == RESULT_OK) { 
     if (requestCode == 1) { 
      Uri si = data.getData(); 
      String[] fpc = {MediaStore.Images.Thumbnails.DATA}; 

      Cursor c = getContentResolver().query(si, fpc, null, null, null); 
      c.moveToFirst(); 

      int ci = c.getColumnIndex(fpc[0]); 
      String fp = c.getString(ci); 
      c.close(); 

      ImageView iv = new ImageView(this); 
      iv.setLayoutParams(new LinearLayout.LayoutParams(100, 100)); 

      iv.setImageBitmap(BitmapFactory.decodeFile(fp)); 
      iv.setScaleType(ImageView.ScaleType.CENTER_CROP);    
      iv.setBackgroundResource(R.drawable.btn_shp_selphoto); 

      LinearLayout lay_gal = (LinearLayout) findViewById(R.id.summ_layout_gal); 
      lay_gal.addView(iv); 
     } 
    } 
} 

答えて

0

私は、コードの一部を変更し、私は私が望んでいた結果を得ました。

Bitmap bm = BitmapFactory.decodeFile(fp); 
Matrix mx = new Matrix(); 

int w = bm.getWidth(); 
int h = bm.getHeight();  

if (w > h) { 
    mx.postScale((float) 98/h, (float) 98/h); 
    bm = Bitmap.createBitmap(bm, (w - h)/2, 0, h, h, mx, true); 
} else { 
    mx.postScale((float) 98/w, (float) 98/w); 
    bm = Bitmap.createBitmap(bm, 0, (h - w)/2, w, w, mx, true); 
} 

iv.setImageBitmap(bm); 
iv.setBackgroundResource(R.drawable.btn_shp_selphoto); 
1
iv.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       v.setBackgroundResource(R.drawable.border04); 
      } 
     }); 

border04.xml:

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item> 
     <shape android:shape="rectangle" android:layout_width="wrap_content"> 
      <stroke android:width="0.5dp" android:color="#FF000000" /> 
      <solid android:color="#FFFFFFFF" /> 
      <padding android:left="2dp" android:top="2dp" android:right="2dp" 
       android:bottom="2dp" /> 
      <corners android:radius="0dp" /> 
     </shape> 
    </item> 


</layer-list> 

別のボーダー(border03.xml):

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item> 
     <shape android:shape="rectangle" android:layout_width="wrap_content"> 
      <stroke android:width="1dp" android:color="#FF000000" /> 
      <solid android:color="#00000000" /> 
      <padding android:left="1dp" android:top="1dp" android:right="1dp" 
       android:bottom="1dp" /> 
      <corners android:radius="1dp" /> 

     </shape> 
    </item> 

    <item android:top="1dp" android:bottom="1dp"> 
     <shape android:shape="rectangle"> 
      <gradient android:startColor="#252525" android:endColor="#252525" 
       android:angle="270" android:centerColor="#545454" /> 
      <!-- border width and color --> 
      <stroke android:width="1dp" android:color="#FFDDDDDD" /> 
     </shape> 
    </item> 

</layer-list> 
関連する問題