4

我有ImageView這是在CardView裏面,如下面我的代碼。我的ImageView scaleType =「fitXY」不適用於前棒棒糖 devices.But它看起來更好棒棒糖設備。請看下面的圖片。
Image 正如我們在上面的圖片中看到的那樣,imageview上有白色的填充物,我想要刪除,我想在預先lolipop和post lolipop設備上看起來相似的圖像。請幫我解決問題。謝謝:)ImageView scaleType =「fitXY」在卡片視圖內的前棒棒糖設備中不起作用| Android

<android.support.v7.widget.CardView 
       android:layout_width="match_parent" 
       android:layout_height="160dp" 
       android:layout_marginRight="10dp" 
       android:layout_marginTop="6dp" 
       app:cardCornerRadius="10dp" 
       card_view:cardUseCompatPadding="false" 
       > 

       <ImageView 
        android:id="@+id/imgChat" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:scaleType="fitXY" 
        /> 
      </android.support.v7.widget.CardView> 
+0

嘿,你可以檢查版本的代碼得到這個類,如果低於21,那麼你做使用具有圓角半徑的矩形自定義圖像視圖並在自定義圖像視圖中設置圖像。 – Vaibhavi

+0

@Vaibhavi感謝您的urs輸入:) –

回答

0

由於這個問題是在沒有短路預棒棒糖設備,我們可以CUS在圓形Imageview中對ImageView進行驗證(如在棒棒糖設備中)。我們可以檢查OS並根據它自定義imageview。

基本上,我們可以使用通用映像加載thisOR滑翔圖像加載器this創建應用程序。

通用映像加載提供的默認RoundedCorners創建的ImageView的角落這樣

ImageLoader imageLoader = ImageLoader.getInstance(); 
     imageLoader.init(ImageLoaderConfiguration.createDefault(MainActivity.this)); 
     DisplayImageOptions options = new DisplayImageOptions.Builder() 
       .displayer(new RoundedBitmapDisplayer(20))/// RoundedBitmapDisplayer IS THE CLASS BY WHICH , WE CAN CREATE THE ROUNDED CORNER OF THE IMAGEVIEW 
       .cacheInMemory(true) 
       .cacheOnDisk(true) 
       .build(); 
     imageLoader.displayImage("http://thedeveloperworldisyours.com/wp-content/uploads/scareface.jpeg", YOUR_IMAGEVIEW,options); 

滑翔圖像加載器,我們需要創建一個自定義類來創建Imageview的弧線,在此link找到自定義圓角類來創建圍繞ImageView

Glide.with(this) 
       .load("http://thedeveloperworldisyours.com/wp-content/uploads/scareface.jpeg") 
       .bitmapTransform(new RoundedCornersTransformation(MainActivity.this,15, 2)) ///RoundedCornersTransformation IS THE CLASS, WHICH YOU NEED TO COPY INSIDE YOURS PROJECT, PLEASE FIND THIS CLASS ON ABOVE LINK AND I AM ALSO PROVIDING THE CLASS FOR IT 
       .into(YOUR_IMAGEVIEW); 
圓角圓弧

用於在滑行的圓角,我們需要將下面類複製我們的項目,我從這個link

import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapShader; 
import android.graphics.Canvas; 
import android.graphics.Paint; 
import android.graphics.RectF; 
import android.graphics.Shader; 

import com.bumptech.glide.Glide; 
import com.bumptech.glide.load.Transformation; 
import com.bumptech.glide.load.engine.Resource; 
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool; 
import com.bumptech.glide.load.resource.bitmap.BitmapResource; 

/** 
* Created by javiergonzalezcabezas on 2/4/16. 
*/ 
public class RoundedCornersTransformation implements Transformation<Bitmap> { 

    public enum CornerType { 
     ALL, 
     TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT, 
     TOP, BOTTOM, LEFT, RIGHT, 
     OTHER_TOP_LEFT, OTHER_TOP_RIGHT, OTHER_BOTTOM_LEFT, OTHER_BOTTOM_RIGHT, 
     DIAGONAL_FROM_TOP_LEFT, DIAGONAL_FROM_TOP_RIGHT 
    } 

    private BitmapPool mBitmapPool; 
    private int mRadius; 
    private int mDiameter; 
    private int mMargin; 
    private CornerType mCornerType; 

    public RoundedCornersTransformation(Context context, int radius, int margin) { 
     this(context, radius, margin, CornerType.ALL); 
    } 

    public RoundedCornersTransformation(BitmapPool pool, int radius, int margin) { 
     this(pool, radius, margin, CornerType.ALL); 
    } 

    public RoundedCornersTransformation(Context context, int radius, int margin, 
             CornerType cornerType) { 
     this(Glide.get(context).getBitmapPool(), radius, margin, cornerType); 
    } 

    public RoundedCornersTransformation(BitmapPool pool, int radius, int margin, 
             CornerType cornerType) { 
     mBitmapPool = pool; 
     mRadius = radius; 
     mDiameter = mRadius * 2; 
     mMargin = margin; 
     mCornerType = cornerType; 
    } 

    @Override 
    public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) { 
     Bitmap source = resource.get(); 

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

     Bitmap bitmap = mBitmapPool.get(width, height, Bitmap.Config.ARGB_8888); 
     if (bitmap == null) { 
      bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 
     } 

     Canvas canvas = new Canvas(bitmap); 
     Paint paint = new Paint(); 
     paint.setAntiAlias(true); 
     paint.setShader(new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)); 
     drawRoundRect(canvas, paint, width, height); 
     return BitmapResource.obtain(bitmap, mBitmapPool); 
    } 

    private void drawRoundRect(Canvas canvas, Paint paint, float width, float height) { 
     float right = width - mMargin; 
     float bottom = height - mMargin; 

     switch (mCornerType) { 
      case ALL: 
       canvas.drawRoundRect(new RectF(mMargin, mMargin, right, bottom), mRadius, mRadius, paint); 
       break; 
      case TOP_LEFT: 
       drawTopLeftRoundRect(canvas, paint, right, bottom); 
       break; 
      case TOP_RIGHT: 
       drawTopRightRoundRect(canvas, paint, right, bottom); 
       break; 
      case BOTTOM_LEFT: 
       drawBottomLeftRoundRect(canvas, paint, right, bottom); 
       break; 
      case BOTTOM_RIGHT: 
       drawBottomRightRoundRect(canvas, paint, right, bottom); 
       break; 
      case TOP: 
       drawTopRoundRect(canvas, paint, right, bottom); 
       break; 
      case BOTTOM: 
       drawBottomRoundRect(canvas, paint, right, bottom); 
       break; 
      case LEFT: 
       drawLeftRoundRect(canvas, paint, right, bottom); 
       break; 
      case RIGHT: 
       drawRightRoundRect(canvas, paint, right, bottom); 
       break; 
      case OTHER_TOP_LEFT: 
       drawOtherTopLeftRoundRect(canvas, paint, right, bottom); 
       break; 
      case OTHER_TOP_RIGHT: 
       drawOtherTopRightRoundRect(canvas, paint, right, bottom); 
       break; 
      case OTHER_BOTTOM_LEFT: 
       drawOtherBottomLeftRoundRect(canvas, paint, right, bottom); 
       break; 
      case OTHER_BOTTOM_RIGHT: 
       drawOtherBottomRightRoundRect(canvas, paint, right, bottom); 
       break; 
      case DIAGONAL_FROM_TOP_LEFT: 
       drawDiagonalFromTopLeftRoundRect(canvas, paint, right, bottom); 
       break; 
      case DIAGONAL_FROM_TOP_RIGHT: 
       drawDiagonalFromTopRightRoundRect(canvas, paint, right, bottom); 
       break; 
      default: 
       canvas.drawRoundRect(new RectF(mMargin, mMargin, right, bottom), mRadius, mRadius, paint); 
       break; 
     } 
    } 

    private void drawTopLeftRoundRect(Canvas canvas, Paint paint, float right, float bottom) { 
     canvas.drawRoundRect(new RectF(mMargin, mMargin, mMargin + mDiameter, mMargin + mDiameter), 
       mRadius, mRadius, paint); 
     canvas.drawRect(new RectF(mMargin, mMargin + mRadius, mMargin + mRadius, bottom), paint); 
     canvas.drawRect(new RectF(mMargin + mRadius, mMargin, right, bottom), paint); 
    } 

    private void drawTopRightRoundRect(Canvas canvas, Paint paint, float right, float bottom) { 
     canvas.drawRoundRect(new RectF(right - mDiameter, mMargin, right, mMargin + mDiameter), mRadius, 
       mRadius, paint); 
     canvas.drawRect(new RectF(mMargin, mMargin, right - mRadius, bottom), paint); 
     canvas.drawRect(new RectF(right - mRadius, mMargin + mRadius, right, bottom), paint); 
    } 

    private void drawBottomLeftRoundRect(Canvas canvas, Paint paint, float right, float bottom) { 
     canvas.drawRoundRect(new RectF(mMargin, bottom - mDiameter, mMargin + mDiameter, bottom), 
       mRadius, mRadius, paint); 
     canvas.drawRect(new RectF(mMargin, mMargin, mMargin + mDiameter, bottom - mRadius), paint); 
     canvas.drawRect(new RectF(mMargin + mRadius, mMargin, right, bottom), paint); 
    } 

    private void drawBottomRightRoundRect(Canvas canvas, Paint paint, float right, float bottom) { 
     canvas.drawRoundRect(new RectF(right - mDiameter, bottom - mDiameter, right, bottom), mRadius, 
       mRadius, paint); 
     canvas.drawRect(new RectF(mMargin, mMargin, right - mRadius, bottom), paint); 
     canvas.drawRect(new RectF(right - mRadius, mMargin, right, bottom - mRadius), paint); 
    } 

    private void drawTopRoundRect(Canvas canvas, Paint paint, float right, float bottom) { 
     canvas.drawRoundRect(new RectF(mMargin, mMargin, right, mMargin + mDiameter), mRadius, mRadius, 
       paint); 
     canvas.drawRect(new RectF(mMargin, mMargin + mRadius, right, bottom), paint); 
    } 

    private void drawBottomRoundRect(Canvas canvas, Paint paint, float right, float bottom) { 
     canvas.drawRoundRect(new RectF(mMargin, bottom - mDiameter, right, bottom), mRadius, mRadius, 
       paint); 
     canvas.drawRect(new RectF(mMargin, mMargin, right, bottom - mRadius), paint); 
    } 

    private void drawLeftRoundRect(Canvas canvas, Paint paint, float right, float bottom) { 
     canvas.drawRoundRect(new RectF(mMargin, mMargin, mMargin + mDiameter, bottom), mRadius, mRadius, 
       paint); 
     canvas.drawRect(new RectF(mMargin + mRadius, mMargin, right, bottom), paint); 
    } 

    private void drawRightRoundRect(Canvas canvas, Paint paint, float right, float bottom) { 
     canvas.drawRoundRect(new RectF(right - mDiameter, mMargin, right, bottom), mRadius, mRadius, 
       paint); 
     canvas.drawRect(new RectF(mMargin, mMargin, right - mRadius, bottom), paint); 
    } 

    private void drawOtherTopLeftRoundRect(Canvas canvas, Paint paint, float right, float bottom) { 
     canvas.drawRoundRect(new RectF(mMargin, bottom - mDiameter, right, bottom), mRadius, mRadius, 
       paint); 
     canvas.drawRoundRect(new RectF(right - mDiameter, mMargin, right, bottom), mRadius, mRadius, 
       paint); 
     canvas.drawRect(new RectF(mMargin, mMargin, right - mRadius, bottom - mRadius), paint); 
    } 

    private void drawOtherTopRightRoundRect(Canvas canvas, Paint paint, float right, float bottom) { 
     canvas.drawRoundRect(new RectF(mMargin, mMargin, mMargin + mDiameter, bottom), mRadius, mRadius, 
       paint); 
     canvas.drawRoundRect(new RectF(mMargin, bottom - mDiameter, right, bottom), mRadius, mRadius, 
       paint); 
     canvas.drawRect(new RectF(mMargin + mRadius, mMargin, right, bottom - mRadius), paint); 
    } 

    private void drawOtherBottomLeftRoundRect(Canvas canvas, Paint paint, float right, float bottom) { 
     canvas.drawRoundRect(new RectF(mMargin, mMargin, right, mMargin + mDiameter), mRadius, mRadius, 
       paint); 
     canvas.drawRoundRect(new RectF(right - mDiameter, mMargin, right, bottom), mRadius, mRadius, 
       paint); 
     canvas.drawRect(new RectF(mMargin, mMargin + mRadius, right - mRadius, bottom), paint); 
    } 

    private void drawOtherBottomRightRoundRect(Canvas canvas, Paint paint, float right, 
               float bottom) { 
     canvas.drawRoundRect(new RectF(mMargin, mMargin, right, mMargin + mDiameter), mRadius, mRadius, 
       paint); 
     canvas.drawRoundRect(new RectF(mMargin, mMargin, mMargin + mDiameter, bottom), mRadius, mRadius, 
       paint); 
     canvas.drawRect(new RectF(mMargin + mRadius, mMargin + mRadius, right, bottom), paint); 
    } 

    private void drawDiagonalFromTopLeftRoundRect(Canvas canvas, Paint paint, float right, 
                float bottom) { 
     canvas.drawRoundRect(new RectF(mMargin, mMargin, mMargin + mDiameter, mMargin + mDiameter), 
       mRadius, mRadius, paint); 
     canvas.drawRoundRect(new RectF(right - mDiameter, bottom - mDiameter, right, bottom), mRadius, 
       mRadius, paint); 
     canvas.drawRect(new RectF(mMargin, mMargin + mRadius, right - mDiameter, bottom), paint); 
     canvas.drawRect(new RectF(mMargin + mDiameter, mMargin, right, bottom - mRadius), paint); 
    } 

    private void drawDiagonalFromTopRightRoundRect(Canvas canvas, Paint paint, float right, 
                float bottom) { 
     canvas.drawRoundRect(new RectF(right - mDiameter, mMargin, right, mMargin + mDiameter), mRadius, 
       mRadius, paint); 
     canvas.drawRoundRect(new RectF(mMargin, bottom - mDiameter, mMargin + mDiameter, bottom), 
       mRadius, mRadius, paint); 
     canvas.drawRect(new RectF(mMargin, mMargin, right - mRadius, bottom - mRadius), paint); 
     canvas.drawRect(new RectF(mMargin + mRadius, mMargin + mRadius, right, bottom), paint); 
    } 

    @Override public String getId() { 
     return "RoundedTransformation(radius=" + mRadius + ", margin=" + mMargin + ", diameter=" 
       + mDiameter + ", cornerType=" + mCornerType.name() + ")"; 
    } 
} 
1

此間隙在預棒棒糖設備CardView內部填充的結果。由於在Api < 21中沒有對Shadows和Clipping CardView的本機支持,因此通過回退解決方案重新創建了這些效果。 現在,如果您在前棒棒糖設備中定義了cardCornerRadiusCardView會將內容填充(等於角半徑值)應用於您的內容,以防止您的內容與Corners重疊。

要禁用此行爲,您可以在XML做到這一點:

card_view:cardPreventCornerOverlap="false" 

或用java:

cardView.setPreventCornerOverlap(false) 

禁用重疊角落填充。

更新:如果你真的想實現棒棒糖預裝置同樣的效果,你可以嘗試自己使用的庫這樣一個剪輯圖像:

https://github.com/vinc3m1/RoundedImageView

,所以當你的內容重疊CardView角落會重疊,您將得到外觀棒棒糖後的設備相同..

+0

是否有解決方案解決它的任何解決方法? –

+0

我更新了答案,讓我知道這是否適用於您。 –

+0

我已經在Cardview card_view中使用了這個:cardPreventCornerOverlap =「false」,它給了我一個新問題,即: - 在沒有任何角落的矩形中顯示新圖像(所有圖像末尾的弧形類型) –

相關問題