How to add custom frames to images programmatically

泄露秘密 提交于 2019-12-06 06:43:50

问题


I want to create an app where i need to add frames to the images.i don't have any idea regrading this.i got one link where the frames are added to the images.could anybody help me.

Here is the link

@Thanks in Advance!!


回答1:


Edit: Gallery OnItemClickListener

gallery.setOnItemClickListener(new OnItemClickListener() {
        Bitmap frame = null, out = null;

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
           Bitmap urImage = BitmapFactory.decodeResource(getResources(),
                    R.drawable.urBackgroundImageID);//edit
            frame = BitmapFactory.decodeResource(getResources(),
                    frames[arg2]);
            out = combineImages(frame, urImage);
            imageView.setImageBitmap(out); //add "out" for ur ImageView
        }
    });

frames[] is array of drawables ie different frames

The following method will combine two images dynamically

public Bitmap combineImages(Bitmap frame, Bitmap image) {

    Bitmap cs = null;
    Bitmap rs = null;

    rs = Bitmap.createScaledBitmap(frame, image.getWidth(),
            image.getHeight(), true);

    cs = Bitmap.createBitmap(rs.getWidth(), rs.getHeight(),
            Bitmap.Config.RGB_565);

    Canvas comboImage = new Canvas(cs);

    comboImage.drawBitmap(image, 0, 0, null);
    comboImage.drawBitmap(rs, 0, 0, null);

    if (rs != null) {
        rs.recycle();
        rs = null;
    }
    Runtime.getRuntime().gc();

    return cs;
}

You can try different integer values in

comboImage.drawBitmap(image, 0, 0, null);
comboImage.drawBitmap(rs, 0, 0, null);

where I have put 0 to get needed frame position on the image.




回答2:


There are many ways. A very simple way is that, you can draw your custom frame over your image in a View's onDraw method, then draw the view in a Bitmap. There is another way, write the frame pixels data to image pixels data then combine a new image with frame, you can use openCV or other third libs to decode images.




回答3:


 <FrameLayout
         android:id="@+id/frame"
         android:layout_width="120dp"
        android:layout_height="120dp"
        android:foreground="@drawable/your frame...">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_margin="10dp"
        android:layout_gravity="center"
        android:src="@drawable/ic_launcher" />
    </FrameLayout>


来源:https://stackoverflow.com/questions/15493044/how-to-add-custom-frames-to-images-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!