Android Studio - 如何创建显示在当前视图上方的透明视图

时间:2015-04-17 23:44:38

标签: android xml android-canvas transparency android-view

所以我有一个活动,有自己的XML文件,我有一个单独的视图,它只使用canvas而且没有xml。代码在这里解释,问题和问题在代码下面。

继承人第一个视图,使用画布做画:

public class BallView extends View {
    public BallView (Context context) {
        super(context);
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();

        ...//create Ball objects
     }
}

实际绘制在画布上的球对象:

public Ball(int xMax, int yMax) {
    bounds = new RectF();
    paint = new Paint();
    ...(initialize variables)
}

public void begin(Canvas canvas){
    bounds.set(...);
    paint.setColor(...);
    canvas.drawOval(bounds, paint);
}

以下是调用上述视图的活动:

public class CircleBounceScreen extends ActionBarActivity{
    View ballView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ballView = new BallView(this);
        setContentView(ballView);
        .... //do stuff
    }

    // some triggermethod
    public void trigger(){
        setContentView(R.id.activity_main);
        //fade the button in
        //wait for the button to be clicked
    }
}

现在,我在activity_main XML文件中有一个按钮

<LinearLayout
    ...alignment stuff >
    <Button android:id="@+id/restart_button"
        ...design stuff
        android:visibility="gone"/>
</LinearLayout>

在ballView执行一段时间后,它会通过监听器知道活动。继承人我想要发生的事情; ballView STAYS IN VIEW,就在后台,XML文件中的按钮显示在它上面。这将允许ballView(基本上冻结)和按钮能够被点击。

我已经查看了堆栈溢出,并且所有解决方案都使用透明活动背景(但随后更改setContentView(activity_main)将消除前一个视图,如果透明或不透明则无关紧要。)或者,它们仅仅定义了所有内容在同一个XML文件中,所以他们可以简单地将SessionContentView设置为该XML文件并且很好。请注意,我不能这样做,因为我的一个视图使用Canvas而另一个使用XML。

那么如何同时显示2个视图,其中一个透明地低于另一个?

1 个答案:

答案 0 :(得分:1)

为什么不在XML中包含球视图?

<RelativeLayout>
    <Button android:id="@+id/restart_button"
        ...design stuff
        android:visibility="gone"/>


    <com.packagename.BallView
        android:id="@id/ball_view"/>
</RelativeLayout>

然后在视图中你可以这样做:

public class CircleBounceScreen extends ActionBarActivity{
    View ballView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        .... //do stuff
    }

    // some triggermethod
    public void trigger(){
        // restart_button set visibility visible
        //fade the button in
        //wait for the button to be clicked
    }
}

您可能需要向BallView添加额外的构造函数以允许它在XML中膨胀:

public class BallView extends View {
    public BallView (Context context) {
        this(context, null);
    }

    BallView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    BallView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();

        ...//create Ball objects
    }
}