保存时,在imageview上保留添加的textview位置

时间:2015-04-30 05:53:29

标签: android canvas bitmap

主要活动有两个按钮,一次是“添加文字”,另一个是“保存图像”。这是主要布局的一部分:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout <!-- cause i couldn't added textview on viewpager, i had to add a relativelayout -->
    android:id="@+id/rlvViewImage"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_weight="1">
    <android.support.v4.view.ViewPager
        android:id="@+id/ivPager"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal" />
</RelativeLayout>
<LinearLayout ... <!-- Add text and Save image buttons are here. -->

这些都是主要活动:

private View.OnClickListener mBtnAddTextClicked = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (findViewById(R.id.addededttxtid1) == null)
            addText();
    }
};

private View.OnClickListener mBtnSaveToOutputClicked = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        saveImage(mRlvViwImage);
    }
};

final OnTouchListener mTxtTouchListener = new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        dragAddedTxtViw(event, v);
        return false;
    }
};

public void dragAddedTxtViw(MotionEvent event, View v) {
    RelativeLayout.LayoutParams params =(android.widget.RelativeLayout.LayoutParams) v
            .getLayoutParams();

    switch (event.getAction()) {
    case MotionEvent.ACTION_MOVE: {
        params.topMargin = (int) event.getRawY() - (v.getHeight());
        params.leftMargin = (int) event.getRawX() - (v.getWidth() / 2);
        v.setLayoutParams(params);
        break;
    }
    case MotionEvent.ACTION_UP: {
        params.topMargin = (int) event.getRawY() - (v.getHeight());
        params.leftMargin = (int) event.getRawX() - (v.getWidth() / 2);
        v.setLayoutParams(params);
        break;
    }
    case MotionEvent.ACTION_DOWN: {
        v.setLayoutParams(params);
        break;
    }
    }
}

private void addText() {
    mEdtText = new EditText(MainActivity.this);
    mEdtText.setText(R.string.add_text_here);
    mEdtText.setId(R.id.addededttxtid1);
    mEdtText.setBackgroundColor(getResources().getColor(
            android.R.color.transparent));
    mEdtText.setOnTouchListener(mTxtTouchListener);
    mEdtText.setLongClickable(false);
    mRlvViwImage = new RelativeLayout(MainActivity.this);
    mRlvViwImage = (RelativeLayout) findViewById(R.id.rlvViewImage);
    mRlvViwImage.addView(mEdtText);
}

private void saveImage(View v) {
Bitmap textBitmap = null;
    EditText mEdtTxtViw = null;
    mEdtTxtViw = (EditText) findViewById(R.id.addededttxtid1);
    mEdtTxtViw.setDrawingCacheEnabled(true);
    mEdtTxtViw.buildDrawingCache(true);
    textBitmap = mEdtTxtViw.getDrawingCache();

    // I get original image here
    Bitmap backBitmap = BitmapFactory.decodeFile(mPagerAdapter.getCurrentImage().getFileName());
    Bitmap lastBitmap = Bitmap.createBitmap(backBitmap.getWidth(),
            backBitmap.getHeight(), backBitmap.getConfig());
    Canvas canvas = new Canvas(lastBitmap);
    canvas.drawBitmap(backBitmap, 0, 0, null);

//Doesn't help too much. 
float ratioW = (float)((float)backBitmap.getWidth()/(float)v.getWidth());
float ratioH = (float)((float)backBitmap.getHeight()/(float)v.getHeight());
/////////////////
        canvas.drawBitmap(textBitmap, (float)((float)mEdtTxtViw.getLeft())*ratioW,(float)((float)mEdtTxtViw.getTop())*ratioH, null);
    }

    File myDir = new File(Environment.getExternalStorageDirectory()
            + File.separator);
    myDir.mkdirs();
    Random generator = new Random();
    int n = 10000;
    n = generator.nextInt(n);
    String fname = "Image-" + n + ".jpg";
    File file = new File(myDir, fname);
    if (file.exists())
        file.delete();
    try {
        FileOutputStream out = new FileOutputStream(file);
        // Canvas c = new Canvas(bitmap);
        // this.getResources().getDrawable(R.drawable.p).draw(c);
        // v.draw(c);
        boolean success = lastBitmap.compress(CompressFormat.PNG, 100, out);
        out.flush();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

例如,原始图像大小为1280 * 800,并且viewpager中显示的RelativeLayout图像(根据设备大小)为1110 * 720。 我添加了一个textview并将其拖到某个地方; enter image description here

问题解决 IF 我在relativeLayout上添加textview并使用rlt.draw(c); 但是保存的图像大小等于显示的视图(1110 * 720)。 使用ratioW和ratioH有点帮助,但它们并不准确。

0 个答案:

没有答案
相关问题