为什么我不能在我的ImageView中使用遮罩滤镜?

时间:2016-01-26 12:14:50

标签: java android graphics

我正在使用发光效果,使用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时,过滤器没有更长的时间被应用。

你可以看到这种模糊的样子:

Image showing the effect

将它与ImageView一起使用,现在你得到一个硬边缘:

Image showing a failure to apply the filter

这里发生了什么?

编辑以获取更多信息:

执行绘图的代码,即使用发光涂料:

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());
    }

将drawable放入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;
    }
}

1 个答案:

答案 0 :(得分:1)

从API 14开始,启用硬件加速时不支持BlurMaskFilters

要解决此问题,请将ImageView's图层类型设置为软件:

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