gpt4 book ai didi

android - 如何保存在android ImageView中显示的图像?

转载 作者:行者123 更新时间:2023-11-29 13:58:05 25 4
gpt4 key购买 nike

我的要求是,

  1. 为可绘制文件夹加载图像,然后在图像周围添加圆角。
  2. 在图像周围填充白色/绿色描边。

我在这里完成了它们,这里我的问题是,当我存储显示在 ImageView 中的图像时,它没有存储。

对于圆角,我使用了下面的代码。

   private BitmapDrawable roundCornered(BitmapDrawable scaledBitmap, int i) {

Bitmap bitmap = scaledBitmap.getBitmap();

result = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
Bitmap.Config.ARGB_8888);
canvas = new Canvas(result);

color = 0xff424242;
paint = new Paint();
rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
rectF = new RectF(rect);
roundPx = i;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);

//Add Stroke code Snippet here

canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
BitmapDrawable finalresult = new BitmapDrawable(result);
saveImage_Rotate_white(finalresult.getBitmap());
return finalresult;
}

在我的 ma​​in.xml 中:

     <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/finalLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/black"
android:orientation="vertical" >

<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/roundcorner"
android:src="@drawable/test" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:text="Click here to Rotate" />

</RelativeLayout>

要使 ImageView 成为圆边,

<shape xmlns:android="http://schemas.android.com/apk/res/android" >

<stroke
android:width="6dp"
android:color="#FFFFFF" />

<padding
android:bottom="3dp"
android:left="3dp"
android:right="3dp"
android:top="3dp" />

<corners android:radius="12dp" />

</shape>

代码的最终结果如下所示

enter image description here

在这里,我必须将此图像保存在我的 SdCard 中。我怎样才能做到这一点。我尝试在 roundCornered() 方法中使用以下代码 fragment ,而不是 xml。这也行不通。

          // for stroke

final Rect rect1 = new Rect((rect.left - 10), (rect.top - 10),
(rect.right + 10), (rect.bottom + 10));
final RectF rectF1 = new RectF(rect1);

Paint p = new Paint();
p.setStrokeWidth(1.0f);
p.setColor(Color.GREEN);
p.setStyle(Style.STROKE);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_OUT));
canvas.drawRoundRect(rectF1, 12, 12, p);

我的整个 Activity 是,

public class BorderDraw extends Activity {
ImageView rec_image;
String rec_path;
RelativeLayout mLayout;
private Bitmap result;
private int color;
private Paint paint;
private Rect rect;
private RectF rectF;
private Canvas canvas;
private float roundPx;
RelativeLayout innerLayout;
static File rotated_File, bordered_file;
Button b1;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b1 = (Button) findViewById(R.id.button1);
b1.setVisibility(View.GONE);
mLayout = (RelativeLayout) findViewById(R.id.finalLayout);
rec_image = (ImageView) findViewById(R.id.imageView1);
rec_path = getIntent().getExtras().getString("image_file");
Log.v("Image Path", rec_path);
// rec_image.setDrawingCacheEnabled(true);
String fname = "RoundCorner.jpg";
rotated_File = new File("/sdcard/" + fname);
bordered_file = new File("/sdcard/cornered_image.png");
BitmapDrawable scaledBitmap = resizeBitmap(rec_path, 200, 200,
rec_image);

BitmapDrawable cornered_bitmap = roundCornered(scaledBitmap, 12);

// saveImage_Rotate_white(cornered_bitmap.getBitmap());
rec_image.setImageDrawable(cornered_bitmap);

System.out.println("ImageView Height" + rec_image.getHeight());
System.out.println("ImageView Width" + rec_image.getWidth());

}

private BitmapDrawable roundCornered(BitmapDrawable scaledBitmap, int i) {

Bitmap bitmap = scaledBitmap.getBitmap();

result = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
Bitmap.Config.ARGB_8888);
canvas = new Canvas(result);

color = 0xff424242;
paint = new Paint();
rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
rectF = new RectF(rect);
roundPx = i;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);

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

paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);

BitmapDrawable finalresult = new BitmapDrawable(result);
saveImage_Rotate_white(finalresult.getBitmap());
return finalresult;
}

private void saveImage_Rotate_white(Bitmap bitmap) {
rec_image.setImageBitmap(bitmap);
final Bitmap myRoundedImage = rec_image.getDrawingCache();

if (bordered_file.exists())
bordered_file.delete();
try {
FileOutputStream out = new FileOutputStream(bordered_file);
myRoundedImage.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();

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

}

private BitmapDrawable resizeBitmap(String rec_path2, int wantedWidth,
int wantedHeight, ImageView view) {
// TODO Auto-generated method stub
BitmapFactory.Options options = new BitmapFactory.Options();
options.inTempStorage = new byte[16 * 1024];

Bitmap rec_bitmap = BitmapFactory.decodeFile(rec_path2, options);

int width = rec_bitmap.getWidth();
int height = rec_bitmap.getHeight();
float xScale = ((float) wantedWidth) / width;
float yScale = ((float) wantedHeight) / height;
float scale = (xScale <= yScale) ? xScale : yScale;
Matrix matrix = new Matrix();
matrix.postScale(scale, scale);
Bitmap scaledBitmap = Bitmap.createBitmap(rec_bitmap, 0, 0, width,
height, matrix, true);
BitmapDrawable finalImage = new BitmapDrawable(scaledBitmap);
width = scaledBitmap.getWidth();
height = scaledBitmap.getHeight();

return finalImage;

}

static void saveImage_Rotate(Bitmap dest2) {

if (rotated_File.exists())
rotated_File.delete();
try {
FileOutputStream out = new FileOutputStream(rotated_File);
dest2.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();

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

}
}

最佳答案

好吧,您可以执行以下操作:在您的 ActivityFragment 中,或任何您正在膨胀 XML 的地方,为您的 View 启用缓存,如下所示:

ImageView myImageView= (ImageView)findViewById(R.id.imageView1);
myImageView.setDrawingCacheEnabled(true);

每当你想检索位图时,你只需像这样调用获取绘图缓存:

final Bitmap myRoundedImage = myImageView.getDrawingCache();

或者你可以这样做:

final Bitmap myRoundedImage = Bitmap.createBitmap( 
myImageView.getWidth(), myImageView.getHeight(), Bitmap.Config.ARGB_8888 );
final Canvas canvas = new Canvas( myRoundedImage );
myImageView.draw( canvas );

一旦你有了你的位图,你所要做的就是将它保存到 SD 卡中,像这样:

try {
FileOutputStream out = new FileOutputStream(filename);
myRoundedImage.compress(Bitmap.CompressFormat.PNG, 90, out);
} catch (Exception e) {
e.printStackTrace();
}

关于android - 如何保存在android ImageView中显示的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11018345/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com