2017-02-28 46 views
1

我想在我的FAB中有一個用戶的個人資料圖片。我嘗試了一個厚顏無恥的方式,給了一個負面的填充,但它不起作用。還有填充。我如何刪除填充並填充FAB?FAB與用戶的個人資料裏面沒有任何填充在Android中

這是我的晶圓廠:

<android.support.design.widget.FloatingActionButton 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="bottom|start" 
      android:src="@drawable/testpropic" 
      android:padding="-50dp" 
      android:elevation="0dp" 
      android:id="@+id/profileFAB" 
      android:stateListAnimator="@null" 
      app:pressedTranslationZ="12dp" 
      android:visibility="gone" 
      /> 

This is what I am getting :

回答

0

有兩種方法可以達到你想要的效果:

  1. 只是簡單地覆蓋了支持Android 庫的原始尺寸加入:

    <dimen name="design_fab_image_size" tools:override="true">48dp</dimen> 
    

    dimens.xml,這將自動覆蓋design_fab_image_size 由庫設置(不得與第三方庫工作)爲How to remove auto padding of FAB button in android?

注意指出:在這種情況下48dp是寬度的浮動動作按鈕。

  • 使用一個ImageView使用所述Picasso庫,而不是一個FloatingActionButton的圓Transformation。您將失去簡單的Fab功能和動畫,但您可以更加靈活地正確設置圖像。正如你在按下按鈕的動畫FAB(即顯示所有四個浮動操作按鈕,具有相同的尺寸和屬性的ImageView取代它,會很容易改變你的XML)
  • 的例子類將是:

    public class CircleTransform implements Transformation { 
        @Override 
        public Bitmap transform(Bitmap source) { 
         int size = Math.min(source.getWidth(), source.getHeight()); 
    
         int x = (source.getWidth() - size)/2; 
         int y = (source.getHeight() - size)/2; 
    
         Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size); 
         if (squaredBitmap != source) { 
          source.recycle(); 
         } 
    
         Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig()); 
    
         Canvas canvas = new Canvas(bitmap); 
         Paint paint = new Paint(); 
         BitmapShader shader = new BitmapShader(squaredBitmap, 
           BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP); 
         paint.setShader(shader); 
         paint.setAntiAlias(true); 
    
         float r = size/2f; 
         canvas.drawCircle(r, r, r, paint); 
    
         squaredBitmap.recycle(); 
         return bitmap; 
        } 
    
        @Override 
        public String key() { 
         return "Circle"; 
        } 
    } 
    

    和使用這種技術與Picasso將是一個例子:

    Picasso.with(context).load(imageUri).transform(new CircleTransform()).into(imageView); 
    

    :​​可以接受字符串URL或資源ID

    更新: 如果你想有一個圓圈裏面FloatingActionButton一圈圖像,使用這兩種技術,但使用FloatingActionButton代替。轉換圖像,然後使用Picasso將其設置爲FloatingActionButton

    +0

    謝謝你詳細解答!嘗試了第一種方法和當然情況得到改善。但是由於我的照片是正方形的,照片上的角落就離開了FAB。我如何解決這個問題? –

    +0

    您必須對兩個答案進行組合,而不是ImageView,它代表了.ica()for picasso,使用您的浮動操作按鈕。因此,在將圖像設置爲浮動動作按鈕之前,應該先將圖像轉換爲圓形。 –

    +0

    您還需要幫助嗎? –

    相關問題