2011-06-30 16 views
6

私はthumb_rounded.xmlファイルを定義し、それをAndroidアプリケーションのres/drawableフォルダに配置しました。このファイルで、私は私のImageViewthatに丸みを帯びた角を取得したい、ニュースの親指が含まれてい丸みを帯びたコーナーがないAndroid ImageView

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > 
<corners android:radius="13.0dip" /> 
</shape> 

と私は私のImageViewの背景として、これを設定しています、しかし、私は丸みを帯びた角を得るいけない、画像が長方形のまま。誰もそれをどうやって得るか考えていますか?私は、この画面と同じ効果を達成したい:

B&H first screen

感謝

T

+0

この記事を見てみてみてください。http://stackoverflow.com/questions/2459916/how-to-make-an -imageview-to-have-round-corners – bradley4

+0

丸みを帯びた角をImageViewに提供することは可能ではないと思います。あなたはこの記事のコードに従いたいと思うでしょう:http://stackoverflow.com/questions/2459916/how-to-make-an-imageview-to-have-rounded-corners – wajiw

+0

例えば、Javaを使用しなければならない... B&Hアンドロイドアプリケーションは、最初の画面で角が丸い長方形の画像を持っています。私は同じ効果を達成したいと思います。 – Thiago

答えて

2

あなたがタグのカップルを欠いています。

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> 
    <solid android:color="#ffffff"/>  

    <stroke android:width="3dp" 
      android:color="#ff000000"/> 

    <padding android:left="1dp" 
      android:top="1dp" 
      android:right="1dp" 
      android:bottom="1dp"/> 

    <corners android:radius="30px"/> 
</shape> 

はあなたがRoundRectShapeを認識している、また

hereを見て:ここでは、新たなサンプルがですか?

+1

あなたのコードは動作しませんでした。それは白い背景が大きい私のイメージより丸い長方形であるべきです...私はRoundRectShapeを見ていきます、ありがとう@Pedro – Thiago

+0

私はちょうどサンプルを投稿していました、私はあなたが望むものに値を適応させていませんでしたが、それを行うには非常に簡単にする必要があります! –

+0

+1 .........それは私にとっても役に立ちます。ありがとう –

20

ここ
@Override 
public void onCreate(Bundle mBundle) { 
super.onCreate(mBundle); 
setContentView(R.layout.main); 

ImageView image = (ImageView) findViewById(R.id.img); 
Bitmap bitImg = BitmapFactory.decodeResource(getResources(), 
    R.drawable.default_profile_image); 
image.setImageBitmap(getRoundedCornerImage(bitImg)); 
} 

public static Bitmap getRoundedCornerImage(Bitmap bitmap) { 
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), 
    bitmap.getHeight(), Config.ARGB_8888); 
Canvas canvas = new Canvas(output); 

final int color = 0xff424242; 
final Paint paint = new Paint(); 
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 
final RectF rectF = new RectF(rect); 
final float roundPx = 100; 

paint.setAntiAlias(true); 
canvas.drawARGB(0, 0, 0, 0); 
paint.setColor(color); 
canvas.drawRoundRect(rectF, roundPx, roundPx, paint); 

paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 
canvas.drawBitmap(bitmap, rect, rect, paint); 

return output; 

} 

、ラウンドコーダーはroundPxに依存している、それを使用しています。

+0

これは動作していないようです – Sarvesh

+0

私のために働いてくれてありがとう – KDeogharkar

+0

丸みを帯びたコーナーでタイルモードを繰り返す方法は? – Goofy

13

マスクとして機能するImageViewの上にビューを配置すると、その丸みを帯びたイメージエフェクトを正確に達成できます。

この例を見てみましょう:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:background="#ffff" 
    android:layout_height="match_parent" > 

<ImageView 
    android:id="@+id/image1" 
    android:layout_width="200dp" 
    android:layout_height="200dp" 
    android:padding="10dp" 
    android:src="@android:color/holo_red_dark" 
    /> 

<View 
    android:layout_width="200dp" 
    android:layout_height="200dp" 
    android:layout_alignLeft="@+id/image1" 
    android:layout_alignTop="@+id/image1" 
    android:layout_margin="00dp" 
    android:background="@drawable/mask" /> 
</RelativeLayout> 

とmask.xmlの描画可能:

<?xml version="1.0" encoding="utf-8"?> 
<shape 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
    <corners android:radius="50dp" />  
    <solid android:color="#0000"/> 
    <stroke android:color="#ffff" android:width="10dp"/> 
</shape> 

二つの重要な事柄:

  1. マスク描画可能のストローク幅をすべきImageViewのパディングに一致する
  2. マスクdrawableは背景色と一致する必要があります。
  3. マスクの角の半径は、ストロークの幅に合わせて調整する必要があります(マスクの背後の画像は表示されません)。

サンプルは以下の画像生成:HTH

enter image description here

+0

背景が堅実な背景ではない場合はどうすればよいですか?このメソッドは、ソリッドな背景を持つ丸いコーナー領域を表示するので、うまくいきません。 –

+0

これは本当ですが、これはバックグラウンドとブレンドするマスクを定義できる場合にのみ機能します(ただし、単色である必要はありません)。 –

+0

それはクールなトリックです。その環境とうまく融合することがわかっている場合に備えて、他のソリューションより優れています。 –

関連する問題