android drawable如何创建这个

时间:2016-10-21 11:53:49

标签: android android-layout

您正在尝试创建此布局,我希望使用带有src的视图指向带有形状和提示的drawaable

title here title here](https://res.cloudinary.com/hashnode/image/upload/v1477050769/ismtsuj44lbvsksjztjh.png)[![enter image description here] 1 将它放在图像视图的回收视图中

<RelativeLayout
        ....
          >
     <View 
        align_parentTop= true
        src= " drawable with an invert tip inside "
        ....
          />
     <ImageView //actual image here 
         ... /> 

但是我可以创建形状i并将其转换为xml,因此它支持所有尺寸

1 个答案:

答案 0 :(得分:0)

您可以使用android:scaleY="-1"属性创建旋转图像,并在Canvas上按drawPath绘制提示,如下所示。 只需根据需要更改scaleType图像和三角形角度。

<强> layout.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/imgInvertTip"
        android:layout_width="match_parent"
        android:layout_height="90dp"
        android:scaleType="centerCrop"
        android:scaleY="-1"/>

    <ImageView
        android:id="@+id/imgPhoto"
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/imgInvertTip"
        android:scaleType="fitXY"/>
</RelativeLayout>

<强> MainActivity.java

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

        final ImageView imgInvertedTip = (ImageView) findViewById(R.id.imgInvertTip);
        ImageView imgPhoto = (ImageView) findViewById(R.id.imgPhoto);

        Drawable photo = AppCompatResources.getDrawable(getApplicationContext(), R.drawable.photo);
        imgInvertedTip.setBackground(photo);
        imgPhoto.setImageDrawable(photo);

        ViewTreeObserver vto = imgInvertedTip.getViewTreeObserver();
        vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            public boolean onPreDraw() {
                imgInvertedTip.getViewTreeObserver().removeOnPreDrawListener(this);

                int width = imgInvertedTip.getMeasuredWidth();
                int height = imgInvertedTip.getMeasuredHeight();
                Bitmap tip = createTip(width, height);

                imgInvertedTip.setImageBitmap(tip);
                return true;
            }
        });
    }

    private Bitmap createTip(int width, int height) {
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(android.graphics.Color.WHITE);
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        paint.setAntiAlias(true);

        int tipStep = height / 2;

        Point a = new Point(tipStep, 0);
        Point b = new Point(tipStep * 2, 90);
        Point c = new Point(tipStep * 3, 0);

        Path path = new Path();
        path.setFillType(Path.FillType.INVERSE_EVEN_ODD);
        path.lineTo(a.x, a.y);
        path.lineTo(b.x, b.y);
        path.lineTo(c.x, c.y);
        path.close();

        Bitmap tip = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(tip);
        canvas.drawPath(path, paint);

        return tip;
    }