android - 如何给图片添加相框?

标签 android image surfaceview android-custom-view picasso

我正在尝试实现框架功能,这样如果我在从图库中捕获/检索后提供图像,我已经完成了这部分,现在我被卡住的地方是我如何合并两个图像相对于框架图像相应!!

现在清楚地给出了合并两个图像的解决方案HereHere

但他们没有解释调整一张图片与另一张图片的行为,在我的例子中,这里有一些例子:

image with frame1

image with frame2

我已经在使用像 picasso 和 EasyImage 这样的库,如果他们能提供帮助?

编辑:

测试框架示例 test frame

最佳答案

我举了个例子。请引用此存储库。 https://github.com/nshmura/TestFrame/

Frame 类合并图片的位图和框架的位图。

public class Frame {

    //filename of frame
    private String mFrameName;

    //Rect of picture area in frame
    private final Rect mPictureRect;

    //degree of rotation to fit picture and frame.
    private final float mRorate;

    public Frame(String frameName,int left, int top, int right, int bottom, float rorate) {
        mFrameName = frameName;
        mPictureRect = new Rect(left, top, right, bottom);
        mRorate = rorate;
    }

    public Bitmap mergeWith(Context context, Bitmap pictureBitmap) {
        Bitmap frameBitmap = AssetsUtil.getBitmapFromAsset(context, mFrameName);

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

        Matrix matrix = getMatrix(pictureBitmap);
        canvas.drawBitmap(pictureBitmap, matrix, null);

        canvas.drawBitmap(frameBitmap, 0, 0, null);

        return bitmap;

    }

    Matrix getMatrix(Bitmap pictureBitmap) {
        float widthRatio = mPictureRect.width() /  (float) pictureBitmap.getWidth();
        float heightRatio = mPictureRect.height() / (float) pictureBitmap.getHeight();

        float ratio;

        if (widthRatio > heightRatio) {
            ratio = widthRatio;

        } else {
            ratio = heightRatio;
        }

        float width = pictureBitmap.getWidth() * ratio;
        float height = pictureBitmap.getHeight() * ratio;
        float left = mPictureRect.left - (width - mPictureRect.width()) / 2f;
        float top = mPictureRect.top - (height - mPictureRect.height()) / 2f;

        Matrix matrix = new Matrix();
        matrix.postRotate(mRorate);
        matrix.postScale(ratio, ratio);
        matrix.postTranslate(left, top);

        return matrix;
    }
}

像这样使用:

//This is sample picture.
//Please take picture form gallery or camera.
Bitmap pictureBitmap = AssetsUtil.getBitmapFromAsset(this, "picture.jpg");

//This is sample frame.
// the number of left, top, right, bottom is the area to show picture.
// last argument is degree of rotation to fit picture and frame.
Frame frameA = new Frame("frame_a.png", 113, 93, 430, 409, 4);
Bitmap mergedBitmap = frameA. mergeWith(this, pictureBitmap);

//showing result bitmap
ImageView imageView = (ImageView) findViewById(R.id.image);
imageView.setImageBitmap(mergedBitmap);

结果如下:

enter image description here

关于android - 如何给图片添加相框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38939902/

相关文章:

java - 在android中使用surfaceview在应用程序中使用相机

android - 清除项目后删除了 R.java

android - 单击通知时启动 Activity

python - 如何获得具有边界条件的图像面积?

jquery - 检查图像是否已使用 jQuery 缓存

android - 背景图像绘制时间太长(Canvas)生涩的 Sprite ......?

Android SurfaceView Canvas 呈现错误的颜色

php - Android + Json通过php将部分spinner值插入mysql

android - 通过 adb shell 和代码理解命令 - Android

html - 对齐多个图像