【发布时间】:2011-09-28 17:40:29
【问题描述】:
我找到了change the opacity of a View 的方法,但实际上我需要将View 变暗。我最好的办法是在上面放一个透明的黑色矩形,然后慢慢增加矩形的不透明度。
你知道更好的方法吗?
public class Page07AnimationView extends ParentPageAnimationView {
private final String TAG = this.getClass().getSimpleName();
private ImageView overlay;
private int mAlpha = 0;
public Page07AnimationView(Context context) {
super(context);
}
public Page07AnimationView(Context context, AttributeSet attrs) {
super(context, attrs);
}
protected void init()
{
overlay = new ImageView(mContext);
overlay.setImageResource(R.drawable.black_background);
overlay.setAlpha(0);
overlay.setWillNotDraw(false);
// make the PageAniSurfaceView focusable so it can handle events
setFocusable(true);
}
protected void draw_bitmaps(Canvas canvas)
{
overlay.draw(canvas);
update_bitmaps();
invalidate();
}
public void update_bitmaps()
{
if(mAlpha < 250)
{
mAlpha += 10;
overlay.setAlpha(mAlpha);
}
}
}
上面的代码并没有达到我的预期。 Page07AnimationView 添加到 FrameLayout 在我需要变暗的视图上。 R.drawable.black_background 指向 787px x 492px 黑色 png 图片。
我添加了overlay.setWillNotDraw(false);,但没有帮助。
我将第一个 setAlpha(0) 更改为 setAlpha(255) 但这没有帮助。
我完全删除了 setAlpha() 调用,但没有帮助。
这种添加PageNNAnimationView 的基本技术一直在绘制Bitmaps,而不是绘制ImageView overlay。 (我会使用 Bitmaps,但它们似乎没有 alpha 组件。)
Edit2:这是上面类的父类:
public class ParentPageAnimationView extends View {
private final String TAG = this.getClass().getSimpleName();
protected Context mContext;
public ParentPageAnimationView(Context context) {
super(context);
mContext = context;
init();
}
public ParentPageAnimationView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
init();
}
protected void init()
{
}
protected void draw_bitmaps(Canvas canvas)
{
// will be overridden by child classes
}
@Override
protected void onDraw(Canvas canvas) {
if(this.getVisibility() == View.VISIBLE)
{
if(canvas != null)
{
draw_bitmaps(canvas);
}
}
}
public void update_bitmaps()
{
// will be overridden by child classes
}
public void elementStarted(PageElement _pageElement) {
// Nothing in parent class
}
public void elementFinished(PageElement mElement) {
// Nothing in parent class
}
}
【问题讨论】:
-
我已经确认 draw_bitmaps 和 update_bitmaps 被重复调用了。
标签: android