如何调整图像大小

时间:2014-07-24 11:28:04

标签: android geometry

我从一个圆圈中的网址获取图像...我不知道如何调整大小并设置成一个图像框架圆...其中有255 * 255圆形尺寸... 你能告诉我如何调整图像框的大小

这是我从url获取图片的代码

try {
          ImageView i = (ImageView)rootView.findViewById(R.id.imageView1);
         // ImageView j=(ImageView)rootView.findViewById(R.id.image_frm);
          Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(image_url).getContent());
          Bitmap resized = Bitmap.createScaledBitmap(bitmap, 700, 700, true);
          Bitmap conv_bm = getRoundedRectBitmap(resized, 700);
          i.setImageBitmap(conv_bm); 
         // j.setImageBitmap(bitmap);
        } catch (MalformedURLException e) {
          e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        }

private Bitmap getRoundedRectBitmap(Bitmap bitmap , int pixels) {
    // TODO Auto-generated method stub
    Bitmap result = null;
     try {
            result = Bitmap.createBitmap(800, 800, Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(result);

            int color = 0xff424242;
            Paint paint = new Paint();
            Rect rect = new Rect(0, 0, 900, 700);

            paint.setAntiAlias(true);
            canvas.drawARGB(0, 0, 0, 0);
            paint.setColor(color);
            canvas.drawCircle(300, 300, 400, paint);
            paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
            canvas.drawBitmap(bitmap, rect, rect, paint);

        } catch (NullPointerException e) {
        } catch (OutOfMemoryError o) {
        }
        return result;
    }

2 个答案:

答案 0 :(得分:0)

您可以使用BitmapFactory

Bitmap resizedbitmap = Bitmap.createScaledBitmap(bitmap, 255, 255, true);

或者您可以inSampleSize = n用于BimapFactory.Options,这意味着图片将比现有图片小n倍[将偶数用作值]

答案 1 :(得分:0)

如果我理解你的问题,你需要将图像放在圆圈中: 您需要将自定义ImageView类设为

public class CustomImageView extends ImageView {

    public static float radius = 50.0f; // you can change this radius according to your need means how much corner radius you want. Initially I used 18.0f for rounded corners. 

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

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

    public CustomImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //float radius = 36.0f;  
        Path clipPath = new Path();
        RectF rect = new RectF(0, 0, this.getWidth(), this.getHeight());
        clipPath.addRoundRect(rect, radius, radius, Path.Direction.CW);
        canvas.clipPath(clipPath);
        super.onDraw(canvas);


    }
}

现在您可以在代码中使用它

thumb_img1 = (CustomImageView) findViewById(R.id.imageView1);

//您可以使用此imageview来加载图像,它将是圆形的。


您也可以在xml中使用:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.ig.Image.CustomImageView
        android:id="@+id/imageView1"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="48dp"
        android:layout_marginTop="24dp"
        android:src="@color/redcolor" />

</RelativeLayout>

请告诉我这是否是你需要的。