一旦放置图像,为什么不能获得图像视图的圆角?

时间:2012-10-17 06:49:14

标签: android android-layout android-selector

这是我的布局代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical" >





        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="5dp"
            android:gravity="center" >

            <TableRow
                android:id="@+id/tableRow1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1" >

                <ImageView
                    android:id="@+id/imageView1"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_margin="5dp"
                    android:layout_weight="1"
                    android:background="@drawable/round"
                    android:scaleType="fitXY" />

                <ImageView
                    android:id="@+id/imageView2"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_margin="5dp"
                    android:layout_weight="1"
                    android:background="@drawable/roundp" 
                    android:scaleType="fitXY"/>
            </TableRow>

            <TableRow
                android:id="@+id/tableRow2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1" >

                <ImageView
                    android:id="@+id/imageView3"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_margin="5dp"
                    android:layout_weight="1"
                    android:background="@drawable/roundp"
                    android:scaleType="fitXY" />

                <ImageView
                    android:id="@+id/imageView4"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_margin="5dp"
                    android:layout_weight="1"
                    android:scaleType="fitXY" />
            </TableRow>
        </TableLayout>

    </LinearLayout>

roundp.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle">
    <corners 
        android:topRightRadius="0dp"
        android:bottomLeftRadius="25dp"
        android:topLeftRadius="25dp"
        android:bottomRightRadius="0dp"/>

   <stroke 
       android:width="2dp"
       android:color="@color/red" />
   <solid 
       android:color="@color/white"/>


</shape>

round.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle">
    <corners 
        android:topRightRadius="25dp"
        android:bottomLeftRadius="0dp"
        android:topLeftRadius="0dp"
        android:bottomRightRadius="25dp"/>

   <stroke 
       android:width="2dp"
       android:color="@color/red" />
   <solid 
       android:color="@color/white"/>


</shape>

如果我没有在ImageView中放置任何图像,我可以看到带有圆角的空ImageView,放置图像后圆角消失,正常的矩形形状显示为图像。如何解决这个问题?

非常感谢任何帮助。

4 个答案:

答案 0 :(得分:0)

将渐变放在xml ..

形状中
<gradient
        android:angle="270"
        android:endColor="#fddd"
        android:startColor="#ffff" />

答案 1 :(得分:0)

您一直在使用layout_weight属性, 但你没有在任何地方使用过weight_sum属性。

答案 2 :(得分:-1)

对于回合,您可以添加 android:padding 来显示边框。

然而,对于 roundp ,除非用代码java处理图像,否则我无法修复它。

答案 3 :(得分:-1)

有代码为图像添加圆角

public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {  

    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);  

    Canvas canvas = new Canvas(output);  

    final int color = 0xff424242;  

    final Paint paint = new Paint();  

    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());  

    final RectF rectF = new RectF(rect);  

    final float roundPx = pixels;  

    paint.setAntiAlias(true);  

    canvas.drawARGB(0, 0, 0, 0);  

    paint.setColor(color);  

    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);  

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));  

    canvas.drawBitmap(bitmap, rect, rect, paint);  <br>
    return output;  

}