我發現如何change the opacity of a View
,但我需要實際變暗View
。我最好的想法是在它上面放一個透明的黑色矩形,然後慢慢增加矩形的不透明度。以編程方式變暗視圖android
你知道更好的方法嗎?
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被重複調用。 –