创建自定义imageview - listview

时间:2015-07-18 03:46:24

标签: android android-imageview

我想为ListView创建以下行,每行有两个图像视图,用户可以触摸每个行(打开Activity)。

enter image description here

我该怎么办?我必须创建自定义ImageView吗?

2 个答案:

答案 0 :(得分:1)

感谢How to create a layout that's split diagonally and the two halves are clickable?

的@Fashizel

布局:

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<FrameLayout
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:id="@+id/frameLayout">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/image1"
        android:src="@drawable/image01"
        />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/image2"
        android:src="@drawable/image02"
        />

</FrameLayout>

MainActivity.java:

public class MainActivity extends ActionBarActivity {

Context mContext = this;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FrameLayout frameLayout = (FrameLayout) findViewById(R.id.frameLayout);
    if (frameLayout != null) {
        frameLayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                if(event.getAction() == MotionEvent.ACTION_DOWN){
                    if(calcPlace(event.getX()) < calcPlace(event.getY())){
                        /// go to onClick for the right triangle
                        Toast.makeText(mContext,"onClick for the right triangle", Toast.LENGTH_LONG).show();
                    }
                    else {
                        /// go to onClick for the left triangle
                        Toast.makeText(mContext,"onClick for the left triangle", Toast.LENGTH_LONG).show();
                    }
                }
                return false;
            }
        });
    }
}

private float calcPlace(float x){
    return 100 - x;
}   

...
}

02图像文件:02个三角形

enter image description here

enter image description here

enter image description here

只是图片视图的演示,您可以用于您的观看

希望这有帮助!

答案 1 :(得分:0)

在某些情况下,这可能会对你有所帮助

Bitmap setBitmapInTriangleShape(Bitmap bitmap1, Bitmap bitmap2,
        Bitmap bitmap3) {
    /*
     * int[] values= new int[2]; mImageView.getLocationOnScreen(values);
     */
    double screenHeightAspect = 2.5;
    Bitmap drawnBitmap = null;

    bitmap1 = Bitmap.createScaledBitmap(bitmap1, screenWidth,
            (int) (screenHeight / screenHeightAspect), true);
    bitmap2 = Bitmap.createScaledBitmap(bitmap2, screenWidth,
            (int) (screenHeight / screenHeightAspect), true);
    bitmap3 = Bitmap.createScaledBitmap(bitmap3, screenWidth,
            (int) (screenHeight / screenHeightAspect), true);

    try {
        drawnBitmap = Bitmap
                .createBitmap(screenWidth,
                        (int) (screenHeight / screenHeightAspect),
                        Config.ARGB_8888);
        Canvas canvas = new Canvas(drawnBitmap);
        canvas.drawColor(Color.TRANSPARENT);

        // ---------------------------------------------------------------
        Paint paint = new Paint();
        paint.setStrokeWidth(4);
        paint.setColor(getResources().getColor(
                R.color.gray_divider_reg_edit_grid_1));
        paint.setStyle(Paint.Style.FILL_AND_STROKE);

        Path path = new Path();
        BitmapShader bms = new BitmapShader(bitmap1, TileMode.CLAMP,
                TileMode.CLAMP);
        paint.setStyle(Style.FILL);
        paint.setShader(bms);
        // bms.setLocalMatrix(matrix);

        // -----------------=for photo 1-----------------------------
        path.reset();
        path.setFillType(Path.FillType.EVEN_ODD);

        path.lineTo(0, 0);
        path.lineTo((int) (screenWidth * 0.80), 0);
        // path.lineTo(0, 15);
        path.lineTo(0, (int) (screenHeight * 0.8706 / screenHeightAspect));
        path.close();
        canvas.drawPath(path, paint);

        Matrix mt = new Matrix();

        canvas.drawBitmap(bitmap1, new Matrix(), null);
        // -------------------for photo 3-----------------------------
        BitmapShader bmsUo = new BitmapShader(bitmap3, TileMode.CLAMP,
                TileMode.CLAMP);
        paint.setStyle(Style.FILL);
        paint.setShader(bmsUo);
        // bms.setLocalMatrix(matrix);

        path.reset();
        path.setFillType(Path.FillType.EVEN_ODD);
        path.moveTo((int) (screenWidth * 0.80), 0);
        path.lineTo((int) (screenWidth * 0.80), 0);
        path.lineTo(screenWidth, 0);
        path.lineTo(screenWidth, (int) (screenHeight / screenHeightAspect));
        // path.lineTo(800,800);
        path.lineTo((int) (screenWidth * 0.88),
                (int) (screenHeight / screenHeightAspect));

        path.lineTo((int) (screenWidth * 0.50),
                (int) (screenHeight * 0.32 / screenHeightAspect));

        path.close();
        canvas.drawPath(path, paint);

        // ---------------------for photo 2-------------------------
        BitmapShader bmsUop = new BitmapShader(bitmap2, TileMode.CLAMP,
                TileMode.CLAMP);
        paint.setStyle(Style.FILL);
        paint.setShader(bmsUop);
        // bmsUop.setLocalMatrix(matrix);
        path.reset();
        path.setFillType(Path.FillType.EVEN_ODD);
        path.moveTo((int) (screenWidth * 0.50),
                (int) (screenHeight * 0.32 / screenHeightAspect));
        path.lineTo((int) (screenWidth * 0.50),
                (int) (screenHeight * 0.32 / screenHeightAspect));
        path.lineTo((int) (screenWidth * 0.88),
                (int) (screenHeight / screenHeightAspect));
        path.lineTo(0, (int) (screenHeight / screenHeightAspect));
        path.lineTo(0, (int) (screenHeight / screenHeightAspect * 0.8706));

        path.close();
        canvas.drawPath(path, paint);

    } catch (Exception e) {
        e.printStackTrace();
    }
    return drawnBitmap;

}

有关详细信息,请访问here