I'm coding a simple app which takes two images 1)Photo and 2)Filter(Image file) and then Filter will be applied on the photo. But I also want to save the Image in internal storage after the images are overlapped. As you can see in below code, in last second line I created Bitmap Object filteredPhoto so it can be used to save the image but it can't be referenced because of In-convertible types.
There are two ways,
1) Convert LayerDrawable Object to Drawable and later reference it to Bitmap Object (by casting)
2) Convert LayerDrawable directly to Bitmap if possible.
Can anyone explain me how to make this happen?
Note : This question may have been asked by others but none of them has replied them properly. Most of them said use Canvas but I can't find how Canvas can help me in my problem so kindly don't mark my question as Duplicate.
Code :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
theImageViewMID = (ImageView) findViewById(R.id.theImageView_MID);
Drawable layers[] = new Drawable[2];
layers[0] = ContextCompat.getDrawable(this, R.drawable.image_1546);
layers[1] = ContextCompat.getDrawable(this, R.drawable.new_filter);
layers[1].setAlpha(75);
LayerDrawable layerDrawable = new LayerDrawable(layers);
theImageViewMID.setImageDrawable(layerDrawable);
layerDrawable.set
Bitmap filteredPhoto = ((BitmapDrawable) LayerDrawable).getBitmap();
//MediaStore.Images.Media.insertImage(getContentResolver(), filteredPhoto, "THIS IS TITLE", "THIS IS DESCRIPTION");
}
Related
I am trying to use the following to change the background to an image stored on the sd card:
String pathName = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+ "/newbackground.jpg";
Resources res = getResources();
Bitmap bitmap = BitmapFactory.decodeFile(pathName);
BitmapDrawable bd = new BitmapDrawable(res, bitmap);
View view = findViewById(R.id.fred);
view.setBackgroundDrawable(bd);
But it keeps crashing.
I´m using setBackgroundDrawable as I want it to work below api 16 and it is deprecated.
I put in a check to see if pathname exists and it does.
Any ideas?
mark
I went upto here to add an image to my android app but there is an error line:
String pathName = "chemistry\2013_03_26\chemistry1.pdf";//Error line
Resources res = getResources();
Bitmap bitmap = BitmapFactory.decodeFile(pathName);
BitmapDrawable bd = new BitmapDrawable(res, bitmap);
View view = findViewById(R.id.container);
view.setBackgroundDrawable(bd);
I think you should use absolute path with getFilesDir() or getExternalFileDir().
You can find more information at clickHere
I have some buttons which i set the background with setBackgroundResource(R.drawable.cal_box_center); and the problem I'm having is that my background is a gradient which has this banding effect (annoying) and I've read that in order to remove this you'll need to set the Bitmap.Config.ARGB_8888. I looked into the API and the way to do this is to use decodeStream etc, but how can i use setBackgroundResource and still set the Config to ARGB_8888?
Thanks in advance.
You can use this code snippet:
// create button
Button btn = new Button(getApplicationContext());
//decode the resource(You can also use decodeStream and other decode method of
//BitmapFactory)
Bitmap btm = BitmapFactory.decodeResource(getResources(), R.drawable.cal_box_center);
//create another copy of your bitmap and specify Config
Bitmap newBtm = btm.copy(Bitmap.Config.ARGB_8888, true);
//use your newBtm to create a BitmapDrawable
BitmapDrawable btmDrwble = new BitmapDrawable(newBtm);
// finally set the drawable as your button's background
btn.setBackgroundDrawable(btmDrwble);
I'm trying to create a dynamic icon menu for my android application.
The icon is made from 2 drawable, a 9-patch background image with the icon image overlay on it.
With reference to overlay two images in android to set an imageview, I've the following code which have the overlay nicely.
Resources res = parent.getResources();
Drawable icon_bg = res.getDrawable(R.drawable.menu_icon_bg);
Drawable icon = res.getDrawable(R.drawable.menu_icon);
// Not working
int int_icon_height = icon.getIntrinsicHeight();
int int_icon_width = icon.getIntrinsicWidth();
Rect rect_icon_bg_bound = new Rect();
rect_icon_bg_bound.left = 0;//(int) Math.round(int_icon_width*-1.5);
rect_icon_bg_bound.right = (int) Math.round(int_icon_width*1.5);
rect_icon_bg_bound.top = 0;//(int)Math.round(int_icon_height*-1.5);
rect_icon_bg_bound.bottom = (int)Math.round(int_icon_height*1.5);
icon_bg.setBounds(rect_icon_bg_bound);
Drawable[] layers = new Drawable[2];
layers[0] = icon_bg;
layers[1] = icon;
LayerDrawable layerDrawable = new LayerDrawable(layers);
ImageView iv_icon_combined = new ImageView(getApplicationContext());
iv_icon_combined.setImageDrawable(layerDrawable);
The problem I'm facing right now is adding a padding so that the icon will have some spacing within the background.
Hope to get some enlightenment here, thanks in advance.
I just ran into this same issue. Take a look at the setLayerInset method on LayerDrawable. It takes the layer level as an argument and then the modifiers for the left, top, right, bottom bounds of the drawable.
This has me baffled. I need to copy the Bitmap from one ImageView into another. I do not want to simply copy one ImageView to another because I need to do some changes to the bitmap on its way over.
Here is some code that doesn't work.
ImageView ivSrc = (ImageView) findViewById(R.id.photo);
ivSrc.setDrawingCacheEnabled(true);
Bitmap bmSrc1 = ivSrc.getDrawingCache(); // will cause nullPointerException
Bitmap bmSrc2 = Bitmap.createBitmap(ivSrc.getDrawingCache());//bmSrc2 will be null
View vSrc = (View) ivSrc.getParent();
vSrc.setDrawingCacheEnabled(true);
Bitmap bmSrc3 = Bitmap.createBitmap(vSrc.getDrawingCache()); //black bitmap
//To test the bitmaps:
ImageView ivDest = (ImageView) findViewById(R.id.photo2);
ivDest.setImageBitmap(bmSrc1); //bmSrc1, 2, 3 results shown above
I have to going about this wrong because doing a copy should be so easy. TIA
Not used the drawing cache, but wouldn't you need to call buildDrawingCache() ?
The way I'd do it:
Bitmap bmSrc1 = ((BitmapDrawable)ivSrc.getDrawable()).getBitmap();
Bitmap bmSrc2 = bmSrc1.copy(bmSrc1.getConfig(), true);
Note that bmSrc2 is mutable, i.e. you can stick it in a Canvas and do whatever you like with it before drawing it somewhere.