how to add layer list on an user uploaded image? - android

i want to add effects from my layer-list on an user upload image.In this code i can only do effect on an image from drawable i want to change that to an user upload image
this is my xml file for effect
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/colorPrimary"/>
<item>
<bitmap android:src="#drawable/icn" android:gravity="center" android:alpha="0.1"/>
</item>
<item android:top="300dp"
android:left="0dp"
>
<rotate
android:fromDegrees="-12">
<shape
android:shape="rectangle">
<solid
android:color="?android:colorBackground"/>
</shape>
</rotate>
</item>
this is my xml file for displaying the final image
<ImageView
android:id="#+id/bgImage"
android:layout_width="match_parent"
android:gravity="center"
android:background="#drawable/background"
android:layout_height="290dp"/>

You can construct a LayerDrawable by code instead of xml.
Just new a LayerDrawable and call its addLayer method to add Drawables, and all the properties you set from xml can also be set in code, such as setLayerInset methods.
For example, define the drawable:
<?xml version="1.0" encoding="utf-8"?>
<rotate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="-12">
<shape
android:shape="rectangle">
<solid
android:color="?android:colorBackground"/>
</shape>
</rotate>
Get the drawable from code:
Drawable myDrawable;
Resources res = getResources();
try {
myDrawable = Drawable.createFromXml(res, res.getXml(R.xml.my_drawable));
} catch (Exception ex) {
Log.e("Error", "Exception loading drawable");
}
Create LayerDrawable and addLayer into it:
LayerDrawable layerDrawable = new LayerDrawable();
layerDrawable.addLayer(userDrawable); // userDrawable is from the user upload image
layerDrawable.addLayer(myDrawable);

You can do it by combine bitmaps of user photo and layer-list drawable.
Here is example:
Bitmap effects = BitmapFactory.decodeResource(context.getResources(), R.drawable.effect_name);
Bitmap[] parts = new Bitmap[2];
parts[0] = userImage; //Bitmap
parts[1] = effects; //Bitmap
Bitmap result = Bitmap.createBitmap(parts[0].getWidth() * 2, parts[0].getHeight() * 2, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(result);
Paint paint = new Paint();
for (int i = 0; i < parts.length; i++) {
canvas.drawBitmap(parts[i], parts[i].getWidth() * (i % 2), parts[i].getHeight() * (i / 2), paint);
}

first of all Create a layer list like this
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/background" />
<item
<shape>
<solid/>
<stroke android:width="1dip" android:color="#225786" />
<corners android:radius="10dip"/>
<padding android:left="0dip" android:top="0dip" android:right="0dip" android:bottom="0dip" />
</shape>
</item>
</layer-list>
just want a background:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/background"
android:tileMode="repeat" >
</bitmap>
And finally add background on ImageView
<ImageView
android:id="#+id/bgImage"
android:layout_width="match_parent"
android:gravity="center"
android:background="#drawable/background"
android:layout_height="290dp"/>

Related

how can i set border as well as background image to a textview in android?

as shown in image i want the border outside image but the image changes everytime as it comes from databaseI hava a textview in which the background images come from database, apart from that image i also want to add border to textview background.
I have used RoundedBitmapDrawable to set background image for textview.
RoundedBitmapDrawable img= getDrawableFromName(getResources().getString(R.string.not_available));
//this line sets the border from .xml file.
mytextview.setBackgroundDrawable(getResources().getDrawable(R.drawable.holiday_dateborder));
//this line sets image to background
mytextview.setBackgroundDrawable(img);
//my method to get image.
public RoundedBitmapDrawable getDrawableFromName(String name){
Resources res = getResources();
int resourceId =getResources().getIdentifier(name,"drawable",getPackageName());
Bitmap src = BitmapFactory.decodeResource(res,resourceId);
RoundedBitmapDrawable dr = RoundedBitmapDrawableFactory.create(res, src);
try {
if(dr.getBitmap()!=null)
{
return dr;
}
}
catch (Exception e)
{
return null;
}
return null;
}
You can use this below code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/bg_border_grey"
android:padding="2dp"
>
<TextView
android:id="#+id/textView"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#mipmap/ic_launcher"
/>
</RelativeLayout>
</RelativeLayout>
bg_border_grey.xml add it in your drawable folder
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="0dp" />
<solid android:color="#android:color/transparent" />
<stroke
android:width="1dp"
android:color="#000000" />
</shape>
Try setting this as a background for a round border on text view:-
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="ring">
<stroke
android:width="2dp"
android:color="#color/blue" />
</shape>

Create button background by color in runtime

In my app, I have the selector below.
Is possible to create a similar selector, by color instead of by image-drawable, but at runtime?
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_window_focused="false"
android:drawable="#drawable/ic_button_orange_normal" />
<item
android:state_pressed="true"
android:drawable="#drawable/ic_button_orange_pressed" />
<item
android:state_focused="true"
android:drawable="#drawable/ic_button_orange_focused" />
<item
android:drawable="#drawable/ic_button_orange_normal" />
</selector>
I have tried the function below to create the selector in runtime, but I need to create a round button, not square.
Is this possible?
private StateListDrawable makeSelector(int color)
{
StateListDrawable res = new StateListDrawable();
res.setExitFadeDuration(400);
res.setAlpha(45);
ShapeDrawable d = new ShapeDrawable();
res.addState(new int[] { android.R.attr.state_pressed }, new ColorDrawable(ColorUtilities.decrease(color, 0x003030)));
res.addState(new int[] {}, new ColorDrawable(Color.TRANSPARENT));
return res;
}
Edit again... I have found the way to create round selectors...
private static ShapeDrawable getRoundShapeDrawable(int color)
{
ShapeDrawable shape_drawable = new ShapeDrawable(new OvalShape());
shape_drawable.getPaint().setColor(color);
return shape_drawable;
}
public static StateListDrawable getRoundShapeSelector(int normal_color, int pressed_color)
{
ShapeDrawable normal_shape_drawable = getRoundShapeDrawable(normal_color), pressed_shape_drawable = getRoundShapeDrawable(pressed_color);
StateListDrawable state_list_drawable = new StateListDrawable();
state_list_drawable.addState(new int[] { android.R.attr.state_pressed }, pressed_shape_drawable);
state_list_drawable.addState(new int[] { android.R.attr.state_focused }, pressed_shape_drawable);
state_list_drawable.addState(new int[] { }, normal_shape_drawable);
return state_list_drawable;
}
You can use "StateListDrawable"
StateListDrawable
and add state in it.
Let me know if you face problem in implementing this.
If you want to create a round button. Then you can use layer-list then add this drawable xml as a Layer.
Eg. How to create round button.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:bottom="0dp"
android:left="0dp"
android:right="0dp"
android:top="0dp">
<shape android:shape="rectangle" >
<corners android:radius="40dp" />
<solid android:color="#fff" />
</shape>
</item>
<item
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp">
<shape android:shape="rectangle" >
<corners android:radius="40dp" />
<solid android:color="#d3d3d3" />
</shape>
</item>
<item
android:bottom="1dp"
android:left="2dp"
android:right="1dp"
android:top="2dp">
<shape android:shape="rectangle" >
<corners android:radius="40dp" />
<solid android:color="#EBEBEB" />
</shape>
</item>
<item
android:id="#+id/wl_btn_bg_shape"
android:bottom="6dp"
android:left="6dp"
android:right="6dp"
android:top="6dp">
<shape android:shape="rectangle" >
<corners android:radius="40dp" />
<solid android:color="#00caa8" />
</shape>
</item>
</layer-list>
create and save about code in drawable as wl_btn_bg.xml
Now you need to add this xml as view background
LayerDrawable layers = (LayerDrawable) context.getResources().getDrawable(R.drawable.wl_btn_bg);
view.setBackground(layers);
and if you want to change the color of selector then you need to add following lines in layers
LayerDrawable layers = (LayerDrawable) context.getResources().getDrawable(R.drawable.wl_btn_bg);
GradientDrawable shape = (GradientDrawable) (layers.findDrawableByLayerId(R.id.wl_btn_bg_shape));
shape.setColor(Color.parseColor("#ff6655"));
Done....

How can I change ALL bitmaps on a layer-list programmatically?

Here is the layer-list, named index.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/ll_23_1015_0" android:top="0dp" android:left="0dp" android:bottom="0dp" android:right="0dp">
<shape android:shape="rectangle">
<size android:width="1205dp" android:height="1795dp"/>
<solid android:color="#FFF"/>
</shape>
</item>
<item android:id="#+id/ll_23_1015_1" android:top="75dp" android:left="50dp" android:bottom="1366dp" android:right="919dp">
<bitmap android:src="#drawable/pic_23" />
</item>
<item android:id="#+id/ll_23_1015_2" android:top="504dp" android:left="50dp" android:bottom="937dp" android:right="919dp">
<bitmap android:src="#drawable/pic_23" />
</item>
<item android:id="#+id/ll_23_1015_3" android:top="933dp" android:left="50dp" android:bottom="508dp" android:right="919dp">
<bitmap android:src="#drawable/pic_23" />
</item>
<item android:id="#+id/ll_23_1015_4" android:top="1362dp" android:left="50dp" android:bottom="79dp" android:right="919dp">
<bitmap android:src="#drawable/pic_23" />
</item>
</layer-list>
and this is my code:
imgVShare = (ImageView) findViewById(R.id.imgVShare);
String path = "/storage/sdcard0/temp_photo.jpg";
Drawable finalPic = Drawable.createFromPath(path);
LayerDrawable myDrawable= (LayerDrawable)getResources().getDrawable(R.drawable.index);
for(int i=0; i<5; i++)
{
String ID = "ll_23_1015_" + (i+1);
int resID = getResources().getIdentifier(ID, "id", getPackageName());
Drawable layer = myDrawable.findDrawableByLayerId(resID);
layer = finalPic;
myDrawable.setDrawableByLayerId(resID, layer);
}
imgVShare.setImageDrawable(myDrawable);
All I want to do is to replace all bitmaps inside layer-list with "temp_photo.jpg" that is on internal storage. This method just replace the last bitmap and clear the rest.....please help....!

How to change color of drawable shapes in android

I am developing small android application in which I set drawable resource as background for linear layout. Now what I want to do change background color of linear layout dynamically, but within drawable resource.
My code looks like :
// bcd.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:endColor="#22000000"
android:startColor="#22000000"
android:angle="270" />
<stroke
android:width="3dp"
android:color="#color/white" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<LinearLayout
android:id="#+id/lin_llt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
and I set background for linear layout in my activity like this...
parentLayout = (LinearLayout) view.findViewById(R.id.lin_llt);
parentLayout.setBackgroundResource(R.drawable.bcd);
Now what I want to do i want to change color of my drawable resource that mean change color of my linear layout with rounded corner and padding define in drawable..
I tried this in following way
ShapeDrawable bgShape = (ShapeDrawable )parentLayout.getBackground();
bgShape.getPaint().setColor(Color.BLACK);
but its not working for me. any other solution .
So how to do it...
Need help...
thank you...
Change the layout color dynamically
LinearLayout Layout = (LinearLayout) findViewById(R.layout.id);
Layout.setBackgroundColor(Color.parseColor("#ffffff"));
Dynamically set the background color gradient
View layout = findViewById(R.id.mainlayout);
GradientDrawable gd = new GradientDrawable(
GradientDrawable.Orientation.TOP_BOTTOM,
new int[] {0xFF616261,0xFF131313});
gd.setCornerRadius(0f);
layout.setBackgroundDrawable(gd);
You could try something like this :
Drawable sampleDrawable = context.getResources().getDrawable(R.drawable.balloons);
sampleDrawable.setColorFilter(new PorterDuffColorFilter(0xffff00,PorterDuff.Mode.MULTIPLY));
and for more you could refer to :
How to change colors of a Drawable in Android?
Change drawable color programmatically
Android: Change Shape Color in runtime
http://pastebin.com/Hd2aU4XC
You could also try this :
private static final int[] FROM_COLOR = new int[]{49, 179, 110};
private static final int THRESHOLD = 3;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.test_colors);
ImageView iv = (ImageView) findViewById(R.id.img);
Drawable d = getResources().getDrawable(RES);
iv.setImageDrawable(adjust(d));
}
private Drawable adjust(Drawable d)
{
int to = Color.RED;
//Need to copy to ensure that the bitmap is mutable.
Bitmap src = ((BitmapDrawable) d).getBitmap();
Bitmap bitmap = src.copy(Bitmap.Config.ARGB_8888, true);
for(int x = 0;x < bitmap.getWidth();x++)
for(int y = 0;y < bitmap.getHeight();y++)
if(match(bitmap.getPixel(x, y)))
bitmap.setPixel(x, y, to);
return new BitmapDrawable(bitmap);
}
private boolean match(int pixel)
{
//There may be a better way to match, but I wanted to do a comparison ignoring
//transparency, so I couldn't just do a direct integer compare.
return Math.abs(Color.red(pixel) - FROM_COLOR[0]) < THRESHOLD && Math.abs(Color.green(pixel) - FROM_COLOR[1]) < THRESHOLD && Math.abs(Color.blue(pixel) - FROM_COLOR[2]) < THRESHOLD;
}
as given in How to change colors of a Drawable in Android?
The following works great for setting the color of the drawable programmatically without changing its shape:
parentLayout.getBackground().setColorFilter(
Color.BLACK,
PorterDuff.Mode.SRC_ATOP
);
use this..
<solid android:color="#e1e1e1" />
<stroke
android:width="2dp"
android:color="#808080" />
<corners android:radius="10dp" />
<padding
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
Also possible way is to use:
val layerDrawable : LayerDrawable = imageView.background as LayerDrawable
val bgShape = layerDrawable.findDrawableByLayerId(R.id.shape_id) as GradientDrawable
bgShape.setColor(ContextCompat.getColor(context, R.color.colorPrimary))
with example (drawable/circle.xml):
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/shape_id">
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="1000dp" />
<solid android:color="#F4B528" />
<padding
android:bottom="4dp"
android:left="4dp"
android:right="4dp"
android:top="4dp" />
</shape>
</item>
</layer-list>
and ImageView:
<ImageView
android:id="#+id/imageView"
android:layout_width="16dp"
android:layout_height="16dp"
android:background="#drawable/circle"
/>
One approach would be to create a second drawable XML with the 2nd color and then change the background of the layout with the 2nd drawable.

How to get a Bitmap from a drawable defined in a xml?

How can I get the bitmap from a xml shape drawable.
What am I doing wrong?
shadow.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:angle="270.0"
android:endColor="#android:color/transparent"
android:startColor="#33000000"
android:type="linear" />
<size android:height="7.0dip" />
</shape>
My method to retrieve the bitmap from drawable:
private Bitmap getBitmap(int id) {
return BitmapFactory.decodeResource(getContext().getResources(), id);
}
getBitmap() is returning null when the id passed in is the shadow.xml drawable id.
This is a fully working solution:
private Bitmap getBitmap(int drawableRes) {
Drawable drawable = getResources().getDrawable(drawableRes);
Canvas canvas = new Canvas();
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
canvas.setBitmap(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
drawable.draw(canvas);
return bitmap;
}
And here is an example:
Bitmap drawableBitmap = getBitmap(R.drawable.circle_shape);
circle_shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<size
android:width="15dp"
android:height="15dp" />
<solid
android:color="#94f5b6" />
<stroke
android:width="2dp"
android:color="#487b5a"/>
</shape>
a ShapeDrawable doesn't have a bitmap associated with it - its sole purpose is to be drawn on a canvas. Until its draw method is called, it has no image. If you can get a canvas element at the place where you need to draw the shadow, you can draw it as a shapeDrawable, otherwise you might need a separate, empty view in your layout with the shadow as a background.
You should add size attribute to your shape drawable for preventing "java.lang.IllegalArgumentException: width and height must be > 0".
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#color/colorAccent" />
<stroke
android:width="1.3dp"
android:color="#color/white" />
<size android:height="24dp" android:width="24dp"/>
</shape>

Categories

Resources