Java 为什么可以';我不能在ImageView中使用遮罩过滤器吗?
我使用的是辉光效果,它使用Java 为什么可以';我不能在ImageView中使用遮罩过滤器吗?,java,android,graphics,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.setCol
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;
}
这与自定义矢量图形一起用于绘制模糊,然后绘制我手表表面的指针。这些都打包成一个可绘制的,这样我就可以在多个地方使用它了。至少我是这么想的!结果表明,即使直接在顶层画布上绘制时效果良好,但当我尝试使用ImageView#setImageDrawable
将完全相同的Drawable
设置到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开始,启用硬件加速时不支持模糊maskfilters
要解决此问题,请将ImageView的
图层类型设置为软件:
holder.image.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
你能在ImageView上涂抹此颜料的地方发布代码吗?@GilMoshayof Done,尽管可能不是你想的那样,因为我从来没有在ImageView上涂抹颜料的地方。绘制可绘制图形时使用了绘画,可绘制图形被添加到ImageView。是的,这就是我的意思。在你将它与ImageView一起使用之前,这种可绘制设备工作得很好?@GilMoshayof是的。第一个屏幕截图是直接绘制在WatchFaceService.Engine提供的表面上。第二个屏幕截图是我在配置活动中看到的。我重构了绘图代码,以便在两个地方都可以使用,但由于某些原因,结果是不同的。我敢肯定,当它是ImageView时,它是绘制在表面上的某些属性,但读取WatchFaceService的代码并不能使它变得更清晰。请尝试将ImageView的图层类型设置为software:holder.image.setLayerType(View.layer\u type\u software,null);