我正在使用发光效果,该效果使用 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;
}
这与自定义 vector 图形一起使用来绘制模糊,然后绘制我的表盘的手。这些都被打包到一个 Drawable
中,以便我可以在多个地方使用它。或者说我是这么想的!事实证明,即使直接绘制到顶级 Canvas 上时效果很好,但当我尝试使用 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 的
图层类型设置为软件:
holder.image.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
关于java - 为什么我的 ImageView 不能使用 mask 滤镜?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35013638/