如何使用ImageView创建边框?

时间:2011-10-29 12:05:45

标签: android imageview border

我有一个应用程序可以选择照片并在ImageView中查看它。 我希望应用程序以与Google+相同的方式显示库中选定的照片。

我尝试使用CENTER_CROPCENTER_INSIDE,但照片会在边框上运行。

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == 1) {
            Uri si = data.getData();
            String[] fpc = {MediaStore.Images.Thumbnails.DATA};

            Cursor c = getContentResolver().query(si, fpc, null, null, null);
            c.moveToFirst();

            int ci = c.getColumnIndex(fpc[0]);
            String fp = c.getString(ci);
            c.close();

            ImageView iv = new ImageView(this);
            iv.setLayoutParams(new LinearLayout.LayoutParams(100, 100));

            iv.setImageBitmap(BitmapFactory.decodeFile(fp));
            iv.setScaleType(ImageView.ScaleType.CENTER_CROP);               
            iv.setBackgroundResource(R.drawable.btn_shp_selphoto);

            LinearLayout lay_gal = (LinearLayout) findViewById(R.id.summ_layout_gal);
            lay_gal.addView(iv);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

iv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                v.setBackgroundResource(R.drawable.border04);
            }
        });

border04.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle" android:layout_width="wrap_content">
            <stroke android:width="0.5dp" android:color="#FF000000" />
            <solid android:color="#FFFFFFFF" />
            <padding android:left="2dp" android:top="2dp" android:right="2dp"
                android:bottom="2dp" />
            <corners android:radius="0dp" />
        </shape>
    </item>


</layer-list>

另一个边框(border03.xml):

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle" android:layout_width="wrap_content">
            <stroke android:width="1dp" android:color="#FF000000" />
            <solid android:color="#00000000" />
            <padding android:left="1dp" android:top="1dp" android:right="1dp"
                android:bottom="1dp" />
            <corners android:radius="1dp" />

        </shape>
    </item>

    <item android:top="1dp" android:bottom="1dp">
        <shape android:shape="rectangle">
            <gradient android:startColor="#252525" android:endColor="#252525"
                android:angle="270" android:centerColor="#545454" />
            <!-- border width and color -->
            <stroke android:width="1dp" android:color="#FFDDDDDD" />
        </shape>
    </item>

</layer-list>

答案 1 :(得分:0)

我改变了一些代码,我得到了我想要的结果。

Bitmap bm = BitmapFactory.decodeFile(fp);
Matrix mx = new Matrix();

int w = bm.getWidth();
int h = bm.getHeight();     

if (w > h) {
    mx.postScale((float) 98 / h, (float) 98 / h);
    bm = Bitmap.createBitmap(bm, (w - h) / 2, 0, h, h, mx, true);
} else {
    mx.postScale((float) 98 / w, (float) 98 / w);
    bm = Bitmap.createBitmap(bm, 0, (h - w) / 2, w, w, mx, true);
}

iv.setImageBitmap(bm);
iv.setBackgroundResource(R.drawable.btn_shp_selphoto);