如何在点击时使布局透明

时间:2014-05-21 07:06:22

标签: android android-layout

我有两个布局和一个imageView。父布局linearLayout1具有图像背景,其子布局relativeLayout1具有深色背景。因此,当我在屏幕上移动imageView时,它会显示linearLayout1图像的选区。我应用了触摸动作,但不知道如何显示选择区域。代码在这里

    public class MainActivity extends Activity {

// RelativeLayout viewRelative;
ImageButton imgBtnTarget;
Animation aniRotate, aniZoom;
PointF downPoint = new PointF();// Record Mouse Position When Pressed Down
PointF strtPointImg = new PointF(); // Record Start Position of 'img'

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    imgBtnTarget = (ImageButton) findViewById(R.id.imageButtontTarget);

    imgBtnTarget.setOnTouchListener( new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // for moving image
            switch (event.getAction()) {
            case MotionEvent.ACTION_MOVE:
                PointF mv = new PointF(event.getX() - downPoint.x, event
                        .getY() - downPoint.y);
                imgBtnTarget.setX((int) (strtPointImg.x + mv.x));
                imgBtnTarget.setY((int) (strtPointImg.y + mv.y));
                strtPointImg = new PointF(imgBtnTarget.getX(), imgBtnTarget.getY());
                break;
            }
            return false;
        }
    });
}

}

这里是xml资源

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/back" >

<RelativeLayout
    android:id="@+id/relativeViewBlack"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/black" >

    <ImageButton
        android:id="@+id/imageButtontTarget"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:background="@android:color/transparent"
        android:scaleType="fitCenter"
        android:src="@drawable/target" />
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

在Java代码中:

LinearLayout ll = (LinearLayout) findViewById(R.id.your_ll_id);
ll.setAlpha(TRANSPARENT_VALUE);

或者:

ll.setBackgroundColor(Color.TRANSPARENT);

如果你想从开头添加到XML中的布局全部透明:

android:background="#00ffffff"

编辑:

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                v.setAlpha(0.5f);
                break;
            case MotionEvent.ACTION_UP:
                v.setAlpha(1.0f);
                break;
            case MotionEvent.ACTION_MOVE:
                //your code...
                break;
            }
            return true;
        }