2017-07-11 22 views
0

2つのImageViewを持つレイアウトが1つあります。そして、実行時にそのimageView(円錐角)にRadiusを設定します。しかし、RadiusをImageView(ギャラリーから選択)に設定すると、四角形で表示されます。選択した画像に半径が適用されない理由このように、実行時にRADIUSを設定してください。、画像が設定されていませんRadiusの後に

LayerDrawable layerDrawable = (LayerDrawable) getResources() 
       .getDrawable(R.drawable.back_image); 
     GradientDrawable gradientDrawable = (GradientDrawable) layerDrawable 
       .findDrawableByLayerId(R.id.gradientDrawble); 
     gradientDrawable.setCornerRadius(70); 

     image1.setBackground(layerDrawable); 
     image2.setBackground(layerDrawable); 

back_image.xml

<item android:id="@+id/gradientDrawble"> 

    <shape android:shape="rectangle" > 
     <solid android:color="@color/lightGrey"/> 
     <corners android:radius="0dp" /> 
    </shape> 
</item> 

<item> 
    <bitmap 
     android:gravity="center" 
     android:src="@drawable/icon" /> 
</item> 

output

+0

imageviewに画像を表示するには、画像ローダーライブラリを使用していますか? – Beena

+0

@Divya Chavadaが私の答えを見て、これがあなたを助けてくれることを願っています。 –

+0

@AndyDeveloperありがとうございます。 : –

答えて

0

私はあなたがCとImageViewのをしたいと仮定しますあなたがここにthis library

を使ってこれを実現することができるようにORNER半径は

<com.makeramen.roundedimageview.RoundedImageView 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/imageView1" 
    android:src="@drawable/photo1" 
    android:scaleType="fitCenter" 
    app:riv_corner_radius="30dip" 
    app:riv_border_width="2dip" 
    app:riv_border_color="#333333" 
    app:riv_mutate_background="true" 
    app:riv_tile_mode="repeat" 
    app:riv_oval="true" /> 

スニペットているか、このカスタムクラスを使用することができます

public class RoundedImageView extends ImageView { 
 
    private Path mMaskPath; 
 
    private Paint mMaskPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 
 
    private int mCornerRadius = 10; 
 

 
    public RoundedImageView(Context context) { 
 
     super(context); 
 

 
     init(context); 
 
    } 
 

 
    public RoundedImageView(Context context, AttributeSet attributeSet) { 
 
     super(context, attributeSet); 
 

 
     init(context); 
 
    } 
 

 
    public RoundedImageView(Context context, AttributeSet attrs, int defStyle) { 
 
     super(context, attrs, defStyle); 
 

 
     init(context); 
 
    } 
 

 
    private void init(Context context) { 
 
     ViewCompat.setLayerType(this, ViewCompat.LAYER_TYPE_SOFTWARE, null); 
 
     mMaskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); 
 
     mMaskPaint.setColor(context.getResources().getColor(R.color.transparent)); 
 

 
     mCornerRadius = (int) context.getResources().getDimension(R.dimen.image_border_curvature); 
 
    } 
 

 
    /** 
 
    * Set the corner radius to use for the RoundedRectangle. 
 
    */ 
 
    public void setCornerRadius(int cornerRadius) { 
 
     mCornerRadius = cornerRadius; 
 
     generateMaskPath(getWidth(), getHeight()); 
 
     invalidate(); 
 
    } 
 

 
    @Override 
 
    protected void onSizeChanged(int w, int h, int oldW, int oldH) { 
 
     super.onSizeChanged(w, h, oldW, oldH); 
 

 
     if (w != oldW || h != oldH) { 
 
      generateMaskPath(w, h); 
 
     } 
 
    } 
 

 
    private void generateMaskPath(int w, int h) { 
 
     mMaskPath = new Path(); 
 
     mMaskPath.addRoundRect(new RectF(0,0,w,h), mCornerRadius, mCornerRadius, Path.Direction.CW); 
 
     mMaskPath.setFillType(Path.FillType.INVERSE_WINDING); 
 
    } 
 

 
    @Override 
 
    protected void onDraw(Canvas canvas) { 
 
     if(canvas.isOpaque()) { // If canvas is opaque, make it transparent 
 
      canvas.saveLayerAlpha(0, 0, canvas.getWidth(), canvas.getHeight(), 255, Canvas.HAS_ALPHA_LAYER_SAVE_FLAG); 
 
     } 
 

 
     super.onDraw(canvas); 
 

 
     if(mMaskPath != null) { 
 
      canvas.drawPath(mMaskPath, mMaskPaint); 
 
     } 
 
    } 
 
}

+0

ありがとうございます。しかし、私はカスタムビューを使用したくありません。 –

0

この短いコードを試してみてください。ライブラリとチャームのように動作する必要はありません。

RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(Activity.this.getResources(), yourbitmap); 
roundedBitmapDrawable.setCornerRadius(setYourOwnRadius); 
roundedBitmapDrawable.setCircular(true); 
your imageview.setImageDrawable(roundedBitmapDrawable); 
関連する問題