Tôi đã tìm thấy cách change the opacity of a View
, nhưng tôi cần phải thực sự làm tối một số View
. Ý tưởng tốt nhất của tôi là đặt một hình chữ nhật màu đen trong suốt lên nó và sau đó từ từ tăng độ mờ đục của hình chữ nhật.lập trình làm tối một Chế độ xem android
Bạn có biết cách làm đẹp hơn không?
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);
}
}
}
Đoạn mã trên không làm những gì tôi đã hy vọng. Page07AnimationView
được thêm vào một FrameLayout
trên màn hình tôi cần phải làm tối. R.drawable.black_background
trỏ đến hình ảnh png đen 787px x 492px.
Tôi đã thêm overlay.setWillNotDraw(false);
nhưng không hiệu quả. Tôi đã thay đổi setAlpha(0)
đầu tiên thành setAlpha(255)
nhưng điều đó không giúp ích gì. Tôi đã xóa hoàn toàn các cuộc gọi setAlpha()
nhưng không giúp được gì.
Kỹ thuật cơ bản này để thêm PageNNAnimationView
đã hoạt động để vẽ Bitmaps
, nhưng không được vẽ ImageView overlay
. (Tôi sẽ sử dụng Bitmaps
, nhưng họ dường như không có một thành phần alpha.)
Edit2: đây là phụ huynh của lớp trên:
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
}
}
Tôi xác nhận rằng draw_bitmaps và update_bitmaps đang được gọi là liên tục. –