簡體   English   中英

為什么我的ImageView不能使用遮罩濾鏡?

[英]Why can't I use a mask filter with my ImageView?

我正在使用使用setMaskFilter來模糊繪制區域的發光效果:

public static Paint createGlowPaint(Context context, @ColorRes int baseColorKey) {
    float glowWidth = context.getResources().getDimension(R.dimen.analog_blur_width);
    Paint paint = new Paint();
    paint.setColor((Workarounds.getColor(context, baseColorKey) & 0xFFFFFF) | 0x40000000);
    paint.setMaskFilter(new BlurMaskFilter(glowWidth, BlurMaskFilter.Blur.NORMAL));
    paint.setStrokeWidth(glowWidth);
    paint.setStrokeCap(Paint.Cap.ROUND);
    paint.setStyle(Paint.Style.FILL_AND_STROKE);
    return paint;
}

這與自定義矢量圖形一起用於繪制模糊,然后繪制我的表盤指針。 這些全部打包成一個Drawable以便我可以在多個地方使用它。 還是我想! 事實證明,即使將其直接繪制到頂層畫布上也能正常工作,但是當我嘗試使用ImageView#setImageDrawable將完全相同的Drawable設置到ImageView ,不再應用濾鏡。

您可以看到這種模糊的外觀:

該圖顯示了效果

將其與ImageView一起使用,現在您會得到一個硬的優勢:

該圖顯示了無法應用過濾器

這里發生了什么?

編輯其他信息:

進行繪圖的代碼,即使用發光塗料:

public abstract class Hands extends Drawable {
    // ... lots of other cruft

    @Override
    public void draw(Canvas canvas) {
        //todo could probably precompute more in onBoundsChange
        Rect bounds = getBounds();
        int centerX = bounds.centerX();
        int centerY = bounds.centerY();

        if (watchMode.isInteractive()) {
            handGlowPath.reset();
            handGlowPath.addPath(hourHand.getPath());
            handGlowPath.addPath(minuteHand.getPath());
            handGlowPath.addCircle(centerX, centerY, centreRadius, Path.Direction.CCW);
            canvas.drawPath(handGlowPath, handGlowPaint);
        }

        hourHand.draw(canvas);
        minuteHand.draw(canvas);

        if (watchMode.isInteractive()) {
            secondHand.draw(canvas);
            if (hasThirds()) {
                thirdHand.draw(canvas);
            }
        }

        canvas.drawCircle(centerX, centerY, centreRadius, centrePaint.getPaint());
    }

將可繪制對象放入ImageView

class PreviewListViewAdapter extends BaseAdapter implements ListAdapter {
    // ... other methods for the list adapter

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView instanceof ViewHolder) {
            holder = (ViewHolder) convertView;
        } else {
            holder = new ViewHolder(getContext(), inflater.inflate(R.layout.config_list_item, parent, false));
        }

        // Deliberately making this a square.
        LinearLayout.LayoutParams holderLayoutParams =
                new LinearLayout.LayoutParams(parent.getWidth(), parent.getWidth());
        LinearLayout.LayoutParams viewLayoutParams =
                new LinearLayout.LayoutParams(parent.getWidth(), parent.getWidth());
        if (position == 0) {
            holderLayoutParams.height += 20 * getResources().getDisplayMetrics().density;
            viewLayoutParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
        } else if (position == getCount() - 1) {
            holderLayoutParams.height += 20 * getResources().getDisplayMetrics().density;
            viewLayoutParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;
        } else {
            viewLayoutParams.gravity = Gravity.CENTER;
        }
        holder.setLayoutParams(holderLayoutParams);
        holder.view.setLayoutParams(viewLayoutParams);

        holder.image.setImageDrawable(items[position].drawable);
        holder.text.setText(items[position].labelText);
        return holder;
    }
}

從API 14開始,啟用硬件加速時不支持BlurMaskFilters

要解決此問題,請將ImageView's圖層類型設置為software:

 holder.image.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM