0

我正在尝试转换RelativeLayout为位图并希望将其与子元素一起保存到电话库中。但是当我保存它时,只有一张图像保存在画廊中,而另一个孩子没有保存。

public class Result extends Activity implements android.view.View.OnClickListener{
    private MaskAdjuster mIV;
    private Bitmap mBitmap,b;
    private Bundle getValues;
    private Button save,moreApp;
    private ImageView mask;
    private BitmapDrawable d;
    private static File APP_FILE_PATH = new File("/sdcard/rISHABH/");
    private int x,y;
    private int []epx=null;
    private int []epy=null;
    private int []lpx=null;
    private int []lpy=null;
    private float eyedistance;
    private int count;
    public String filename;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    mIV=new MaskAdjuster(this);
    setContentView(R.layout.result);
    initialize();
    b=getIntent().getParcelableExtra("bitmap");
    mBitmap=b.copy(Bitmap.Config.RGB_565, true);
    getValues=getIntent().getBundleExtra("bundle");
    epx=getValues.getIntArray("eyelocationX");
    epy=getValues.getIntArray("eyelocationY");
    lpx=getValues.getIntArray("liplocationX");
    lpy=getValues.getIntArray("liplocationY");


    eyedistance=getValues.getFloat("eyedistance");
    count=getValues.getInt("count");
    mIV=(MaskAdjuster)findViewById(R.id.iv_resultface);
    mIV.setImageBitmap(mBitmap);
    mIV.setDisplayPoints(epx, epy,lpx,lpy, count * 2, eyedistance);
    mIV.invalidate();
    mask.setImageBitmap(MaskAdjuster.getFace());
    save.setOnClickListener(this);
    moreApp.setOnClickListener(this);
    }


    private void initialize() {
    save=(Button)findViewById(R.id.btn_save);
    moreApp=(Button)findViewById(R.id.btn_more_app);
    mask=(ImageView)findViewById(R.id.iv_mask);
    }


    @Override
    protected void onPause() {
    // TODO Auto-generated method stub

    super.onPause();

    b.recycle();
    finish();

    }


    @Override
    public void onClick(View v) {
    switch (v.getId()) {
    case R.id.btn_save:

    try{
    saveandforward();


    Toast.makeText (getApplicationContext(), 
    "Image has been saved to Gallery(SD card) by name : " + APP_FILE_PATH +            filename, Toast.LENGTH_LONG).show ();

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

    break;
    case R.id.btn_more_app:

    break;

    default:
    break;
    }
    }



    void saveandforward(){
    Bitmap bm=null;
    try {
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat ("yyyyMMddHHmmss");
    filename = sdf.format(cal.getTime());

    final FileOutputStream out = new FileOutputStream(new File      (APP_FILE_PATH     +filename+".png"));
    try{
    View vs = (View)findViewById(R.id.Frame_layout);
    bm = loadBitmapFromView(vs);

    }
    catch(Exception e)
    {
    Log.e("meesge",e.getMessage()); 

    }
    bm.compress(Bitmap.CompressFormat.PNG, 90, out);
    out.flush();
    out.close();

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

    }
    public static Bitmap loadBitmapFromView(View v) {
    Bitmap b = Bitmap.createBitmap(320,480, Bitmap.Config.RGB_565);
    Canvas c = new Canvas(b);
    v.draw(c);
    return b;

         }
    }

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"
android:background="@drawable/bg">
<FrameLayout
android:id="@+id/Frame_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
>
<com.shagun.agemyface.MaskAdjuster
android:id="@+id/iv_resultface"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageView
android:id="@+id/iv_mask"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

/>
</FrameLayout>
<Button 
android:id="@+id/btn_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="@drawable/save"/>

<Button 
android:id="@+id/btn_more_app"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="More App"/>




</RelativeLayout>
4

3 回答 3

2

我猜你喜欢创建布局的屏幕截图请尝试使用它。

RelativeLayout layout = (RelativeLayout)findViewById(R.id.x);
layout.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(layout.getDrawingCache());
layout.setDrawingCacheEnabled(false);
于 2013-04-16T07:09:18.020 回答
0

问题是您实际上是在保存FrameLayout代码。这是您的问题所在:

View vs = (View)findViewById(R.id.Frame_layout);

为您添加一些 idRelativeLayout并将行更改为:

View vs = findViewById(R.id.Id_Of_Relative_Layout);
于 2013-04-16T07:06:00.350 回答
0
    RelativeLayout rlMain = (RelativeLayout)findViewById(R.id.abc);

    rlMain.setDrawingCacheEnabled(true);
    Bitmap bitmap = rlMain.getDrawingCache();


    String file = getFilename();

    try {

        FileOutputStream ostream = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, ostream);
        ostream.close();
        rlMain.invalidate();

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        rlMain.setDrawingCacheEnabled(false);
    }



    private String getFilename() {
    File file = new File(IMAGE_PATH);
    if (!file.exists()) {
        file.mkdirs();
    }
    String uriSting = (file.getAbsolutePath() + "/"
            + System.currentTimeMillis() + ".jpg");
    return uriSting;
}
于 2017-08-01T12:31:45.643 回答