2016-09-30 35 views
5

我在畫布上創建了一個圓形剪輯路徑,並且在畫布上有一列動畫數字,所以我看到剪輯部分的動畫數字和動畫。 這裏是剪輯代碼:Android Nougat clipPath在動畫過程中不工作

mClipPath.addCircle((w/2f), (h/2f), radius, Path.Direction.CW); 
canvas.clipPath(mClipPath, Region.Op.INTERSECT); 

正如你可以看到,0動畫出來,1在動畫(右邊的數字)。 enter image description here

但是在某些情況下,剪輯在動畫過程中(左側3和4)不會正確發生。

enter image description here

這是牛軋糖只發生。

+0

可以共享其他的API版本的屏幕截圖?另外,更多的代碼會有所幫助 – Shmuel

+0

其他api版本與第一個屏幕截圖相似,在動畫過程中沒有任何數字的不規則剪切。 – Santoshastagi

回答

1

我看到同樣的問題。在滾動包含下面的CircleCropDrawable的RecyclerView時,根據滾動位置的不同,我可以看到剪切後的剪貼畫和未剪裁的剪貼畫。

但是,通過反覆試驗,我發現只有當路徑與drawable的邊相交時纔會發生這種情況。減少由1DP半徑修復該問題(比1DP少不工作)

CircleCropDrawable.java

package com.someone.android.view; 
 

 
import android.content.Context; 
 
import android.graphics.Canvas; 
 
import android.graphics.ColorFilter; 
 
import android.graphics.Path; 
 
import android.graphics.Rect; 
 
import android.graphics.drawable.Drawable; 
 
import android.support.annotation.NonNull; 
 
import android.util.TypedValue; 
 

 
public class CircleCropDrawable extends Drawable { 
 

 
    private final Context mContext; 
 
    private final Drawable mDrawable; 
 

 
    public CircleCropDrawable(Context context, Drawable drawable) { 
 
     mContext = context; 
 
     this.mDrawable = drawable; 
 
    } 
 

 
    @Override 
 
    public void draw(@NonNull Canvas canvas) { 
 
     final float width = getBounds().width(); 
 
     final float height = getBounds().height(); 
 
     final Path path = new Path(); 
 
     // There seems to be a bug in N where the clip path doesn't work when animating 
 
     // without a 1dp reduction in radius, the drawable unclips when scrolling in a RecyclerView 
 
     float inset = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, mContext.getResources().getDisplayMetrics()); 
 
     path.addCircle(width/2, height/2, Math.min(width/2, height/2) - inset, Path.Direction.CW); 
 
     canvas.clipPath(path); 
 
     mDrawable.draw(canvas); 
 
    } 
 

 
    @Override 
 
    protected void onBoundsChange(Rect bounds) { 
 
     mDrawable.setBounds(bounds); 
 
    } 
 

 
    @Override 
 
    public int getIntrinsicWidth() { 
 
     return mDrawable.getIntrinsicWidth(); 
 
    } 
 

 
    @Override 
 
    public int getIntrinsicHeight() { 
 
     return mDrawable.getIntrinsicHeight(); 
 
    } 
 

 

 
    @Override 
 
    public void setAlpha(int i) { 
 
     mDrawable.setAlpha(i); 
 
    } 
 

 
    @Override 
 
    public void setColorFilter(ColorFilter colorFilter) { 
 
     mDrawable.setColorFilter(colorFilter); 
 
    } 
 

 
    @Override 
 
    public int getOpacity() { 
 
     return mDrawable.getOpacity(); 
 
    } 
 
}

+0

令人驚歎!我花了很多時間在同一個問題上。我無法弄清楚這一點! :-) 謝謝。 –

0

我們固定通過禁用硬件加速這一觀點,像這樣的問題:

myView.setLayerType(View.LAYER_TYPE_SOFTWARE, null); 
相關問題