Android 不規則圖形佈局的一種實現方法

1. 自定義佈局:

public class ImageClip extends FrameLayout {
    private int width = -1;
    private int height = -1;
    private Bitmap bitmap;

    public ImageClip(Context context) {
        super(context);
    }

    public ImageClip(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ImageClip(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() != MotionEvent.ACTION_DOWN) {
            return super.onTouchEvent(event);
        }

        int x = (int) event.getX();
        int y = (int) event.getY();

        if (width == -1 || height == -1) {
            Drawable drawable = getBackground().getCurrent();
            bitmap = ((BitmapDrawable) drawable).getBitmap();
            width = getWidth();
            height = getHeight();
        }
        if (bitmap == null || x < 0 || y < 0 || x >= width || y >= height) {
            return false;
        }

        int pixel = bitmap.getPixel(x, y);
        if (pixel == Color.TRANSPARENT) {
            return false;
        }
        return super.onTouchEvent(event);
    }
}

2. 佈局文件:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:orientation="vertical">

    <com.android.test1.ImageClip
        android:id="@+id/img_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/left" />

    <com.android.test1.ImageClip
        android:id="@+id/img_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/right" />

    <com.android.test1.ImageClip
        android:id="@+id/img_down"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/down" />
</FrameLayout>

3. Demo Activity:

public class MainActivity extends Activity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.img_left).setOnClickListener(this);
        findViewById(R.id.img_right).setOnClickListener(this);
        findViewById(R.id.img_down).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_left:
                break;
            case R.id.img_right:
                break;
            case R.id.img_down:
                break;
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章