I use function drawCircle(int diameter) to return ShapeDrawable object, which is an OvalShape. Then I use ImageView.setImageDrawable() to set the OvalShape. But the ImageViews are still empty. How to do it correctly?
Function drawCircle():
public ShapeDrawable drawCircle (int diameter) {
OvalShape ovalShape = new OvalShape();
ShapeDrawable drawable = new ShapeDrawable(ovalShape);
drawable.getPaint().setColor(getResources().getColor(R.color.textColorHint));
drawable.getPaint().setStyle(Paint.Style.FILL_AND_STROKE);
drawable.getPaint().setStrokeWidth((float)2);
drawable.setBounds(0 ,0, diameter, diameter);
return drawable;
}
Set ImageView:
one_img.setImageDrawable(drawCircle(selector_one_diameter));
two_img.setImageDrawable(drawCircle(selector_two_diameter));
Above all, I tried using .xml file to describe oval shapes. But since values in dimens.xml cannot be re-written in Java code, I can't dynamically change the size of oval shapes. It couldn't be better if you can suggest another way!
It could be due to you have not set widht, height to your drawable. You can set a default size using below code.
drawable.setIntrinsicWidth();
drawable.setIntrinsicHeight();
Related
I am trying to better understand how layer drawables work within a buttons drawable(s).
I am trying to draw 2 simple colored boxes, one without insets so that it fills the entire button drawable area. And one with some inset.
ColorDrawable background1 = new ColorDrawable(Color.BLUE);
ColorDrawable background2 = new ColorDrawable(Color.GREEN);
Drawable[] drawables = new Drawable[] {
background1,
background2
};
LayerDrawable ld = new LayerDrawable(drawables);
ld.setLayerInset(0, 0, 0, 0, 0 ); // no inset on white box
ld.setLayerInset(1, 8, 8, 8, 8 ); // 8 inset on all side on green box
// set the left drawable on the button
button.setCompoundDrawablesWithIntrinsicBounds(ld, null, null, null);
However that doesn't work at all. The first problem is that the boxes are not filling any area. Is that because a buttons drawables(s) don't have a predefined size? If that is the case I tried to set the bound manually on the boxes, but didn't have much luck either.
Can anyone help me understand what I am doing wrong?
Create a Specific Design in Drawable and call in background button in xml
The problem right now with ColorDrawable is that getIntrinsicWidth()/getIntrinsicHeight() that determine the bounds of the drawable are by default -1, thus it doesn't render on the screen. What would help in your case is to extend ColorDrawable and override the height and width from the constructor.
Here's a sample:
class CustomDrawable(color: Int, val h: Int, val w: Int): ColorDrawable(color) {
override fun getIntrinsicHeight(): Int {
return h
}
override fun getIntrinsicWidth(): Int {
return w
}
}
and then you can instantiate this instead of ColorDrawable like so
val background1 = CustomDrawable(Color.BLUE, dimensionInPx , dimensionInPx)
val background2 = CustomDrawable(Color.GREEN, dimensionInPx, dimensionInPx)
be sure to convert dp to px before passing these values
Hope this helps.
Currently I am trying to create a custom Seekbar without using xml as the images for the background and progress bar need to be changed. This is what I have at the moment:
Drawable drawable = new BitmapDrawable(appState.images.get(Integer.parseInt(image)));
this.setBackgroundDrawable(drawable);
Drawable drawable2 = new BitmapDrawable(appState.images.get(Integer.parseInt(image_a)));
this.setProgressDrawable(drawable2);
this.setProgress(0);
But the progressDrawable is just set to the entire size of Seekbar and when I move the thumb from side to side nothing happens. Does anybody have any ideas as I am completely stumped.
So this previous answer does the trick for you:
Changing the size of the seekbar programmatically but cant get the thumb to be larger than the bar
From the original asker:
In the end I am using this code:
public void setSeekBarImages(){
Drawable drawable = new
BitmapDrawable(appState.images.get(Integer.parseInt(image_a)));
ClipDrawable clip = new ClipDrawable(drawable,
Gravity.LEFT,ClipDrawable.HORIZONTAL);
Drawable drawable2 = new
BitmapDrawable(appState.images.get(Integer.parseInt(image)));
InsetDrawable d1= new InsetDrawable(drawable2,5,5,5,5);
//the padding u want to use
setThumb(null);
LayerDrawable mylayer = new LayerDrawable(new Drawable[]{d1,clip});
setProgressDrawable(mylayer);
setProgress(0);
}
I need a little help with the thing that I want to achieve. I'm using BitmapShader in my application to draw on a canvas. I'm setting a custom png file as shader to my paint variable and I want to change the color of shader.
Here is an example code which I'm using :
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.particle_point);
BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
mPaint.setShader(shader);
ColorFilter filter = new LightingColorFilter(0xFFFFFFFF , 0x000000FF );
mPaint.setColorFilter(filter);
I find that I can change it's color by using :
ColorFilter filter = new LightingColorFilter(0xFFFFFFFF , 0x000000FF );
, but I need to be able to change it's color by using a custom color picker,which returns color code similar to this : -234423123.
So is there any way that I can use this color code and set it as color to my paint variable.
Thanks in advance!
The color you are getting converted to hex is: FFFFFFFFF206FCAD. So youst need to get rid of the 8 leading Fs:
int color = -234423123;//0xFFFFFFFFF206FCAD
int myColor = 0x00000000FFFFFFFF & color;
myColor should be ok.
Just to add a little bit more detailed to the Moss's answer. As he suggest you can use myColor as the value you want and to set the right value to your shader you have to add myColor to your LightingColorFilter like this :
ColorFilter filter = new LightingColorFilter(myColor , myColor );
And it should work.
To get the hex String:
"#"+Integer.toHexString(n));
But your color picker just returns the color's int value which should be enough to work with!
ColorFilter filter = new LightingColorFilter(0xFFFFFFFF , 0x000000FF );
Just change the value which represents the color to the int your colorpicker returns... (without 0x in front of it ofcourse)...
If I ain't wrong this should work just as fine!
I,m trying to paint circle in background of my textView. It should by a small object in up/left corner.
temporar = new TextView(cxt);
OvalShape oval = new OvalShape();
//oval.resize(10, 10);
qualitySignalDrawable = new ShapeDrawable(oval);
qualitySignalDrawable.getPaint().setColor(0xff74AC23);
qualitySignalDrawable.setBounds(2,2,10,10);
signalQualityText.setGravity(Gravity.CLIP_HORIZONTAL);
temporar.setBackgroundDrawable(qualitySignalDrawable);
temporar.setText("aaaaaaaaaaaaa");
I tried set difrents gravity and size and Bounds but id allways stretch shape to container size. I'm missing something?
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.