保存时,将添加的文本视图位置保留在图像视图上

2024-02-21

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

<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();
    }
}

For example, original image size is 1280*800 and RelativeLayout shown image in viewpager is (according to device size) 1110*720. I add a textview and drag it in somewhere; enter image description here

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


None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

保存时,将添加的文本视图位置保留在图像视图上 的相关文章

随机推荐