I have TextView and I want to draw Rounded Rectangle shape programmatically and I want to set that shape as a Background of TextView? and I want to change it colors dynamically, I have posted the picture ?
public static void makeRoundCorner(int bgcolor,int radius,View v,int strokeWidth,int strokeColor)
{
GradientDrawable gdDefault = new GradientDrawable();
gdDefault.setColor(bgcolor);
gdDefault.setCornerRadius(radius);
gdDefault.setStroke(strokeWidth, strokeColor);
v.setBackgroundDrawable(gdDefault);
}
here View v = your textview or button or anything.
Related
I have two column of textView containing some text.
When you click in one text then click in another from other column it should draw a line between them.
I have put a relative layout between the two textView columns and adding a customised view to it.
I debugged and found that its calling the onDraw to draw line but i can not see any lines
Here is my code for that.
public class DrawLineView extends View {
Paint paint = new Paint();
View startView;
View endView;
public DrawLineView(Context context, View startView, View endView) {
super(context);
paint.setColor(Color.BLACK);
this.startView = startView;
this.endView = endView;
invalidate();
}
public DrawLineView(Context context) {
super(context);
}
// public void drawLineNow() {
//
// invalidate();
// }
#SuppressLint("NewApi")
public void onDraw(Canvas canvas) {
canvas.drawLine(startView.getX() + 25, startView.getY() + 50, endView.getX() + 25, endView.getY(), paint);
}
}
This is called in something like this.
selectedTextFromColumn2 = texviewC2T4;
selectedTag2 = list2.get(3).getTag();
if (selectedTag1 == selectedTag2) {
drawLineView = new DrawLineView(MatchingContentScreen.this,selectedTextFromColumn1, selectedTextFromColumn2);
relativeLayout.addView(drawLineView);
drawLineView.invalidate();
}
I am new to canvas .So i am confused what i am doing wrong or missing.
the line coming as a vertical line
I saw this drawing solution from another post.
How to draw a line in android between two or more RadioButtons
Can anyone please help me ?
I want it something like this
I can draw line like this if canvas is on top of these textviews
using
canvas.drawLine(startX , startY , stopX , stopY, paint);
but i can not validate the text or read the textview to match.
I'm a newbe when it comes to Android coding. As a first project I decided to port ZX Spectrum game. I have prepared bitmap frames of the car and buttons that are about to be scaled depending on screen size. I've put the images both in "drawable" and "drawable-hdpi" folders and it seems to work on my LG Leon phone, but not working on other phones. It looks like the image isn't scaled.
https://mechanism.fr/misc/not_scaled.png
here's my code:
declaration of the variables:
Rect source1;
Rect dest1;
Rect arrow_box;
Rect left_box;
Rect right_box;
Rect up_box1;
Rect down_box1;
Rect up_box2;
Rect down_box2;
example of calculating one pair of the Rect dimensions:
private void calculateDimensions(){
(...)
source1=new Rect(0,0,128,100);
arrow_box=new Rect(0,0,300,300);
left_box=new Rect((int)(0+boxxy/2),(int)(wysokosc*0.7),(int)(0+boxxy/2+boxxy),(int)(wysokosc*0.7+boxxy));
and the code responsible for drawing:
Paint drawPaint2 = new Paint();
canvas.drawBitmap(arrow_left_off,arrow_box,left_box,drawPaint2);
canvas.drawBitmap(arrow_right_off,arrow_box,right_box,drawPaint2);
canvas.drawBitmap(arrow_up_off,arrow_box,up_box1,drawPaint2);
canvas.drawBitmap(arrow_down_off,arrow_box,down_box1,drawPaint2);
canvas.drawBitmap(arrow_up_off,arrow_box,up_box2,drawPaint2);
canvas.drawBitmap(arrow_down_off,arrow_box,down_box2,drawPaint2);
the dimensions are calculated after creating the canvas view:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
kanwas=new CanvasV(this);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(kanwas);
getDimensions();
calculateDimensions();
updateScreen();
}
I don't quite understand what seems to be the problem.
I tried to copy the images both to "drawable" and "drawable-hdpi" folders
Also, scale the bitmaps. Scaling the canvas doesn't do anything.
Example of scaling bitmap
bitmap=Bitmap.createScaledBitmap(src, dstWidth, dstHeight, filter)
I have found very few tutorials on how to use the android canvas. However, after looking at the official documentation I was able to produce this code. I simply would like to draw a green rectangle.
public class LevelActivity extends AppCompatActivity {
Object shape;
Target target;
Rect r;
Paint paint;
Bitmap b = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_level);
paint = new Paint();
paint.setColor(Color.GREEN);
paint.setStyle(Paint.Style.FILL);
r = new Rect();
r.set(0,0,c.getWidth(),c.getHeight()/2);
c.drawRect(r,paint);
shape = new Shape(30,10); //Object setup
//shape.pick(); //Pick random point for target
}
}
I do not want to use a customView to draw, I just want to use pure android canvas. When I run the code I get no errors or any warnings.
Any help is greatly appreciated.
-Kelton
Well, you have a Canvas c with an underlying Bitmap b. Now anything that you draw on the Canvas c will actually be drawn on the Bitmap b. And you have done the drawing part in the above code. The Bitmap b has your drawn rectangle. But now to view that you need to load this Bitmap b in some view, like an ImageView. You can have an ImageView in your activity_level layout and then use ImageView.setImageBitmap(b) to load the Bitmap onto the view.
you need to add the following 2 lines at the end:
ImageView iv= (ImageView) findViewById(R.id.imageview_where_i_will_put_my_awesome_green_rectangle);
iv.setImageBitmap(b);
You have to create a ImageView and set the Bitmap as the one you created with canvas.
ImageView imageView = new ImageView(this);
imageView.setBitmap(bitmap);
addContentView(imageView, new LayoutParams(MATCH_PARENT, MATCH_PARENT));
I am creating a game with a few friends, and I am currently working on the main menu. I figured out how to get regular old TextButtons, but ImageTextButtons are giving me a very hard time. I have two microphone images, one on and one off, and it should change when the user clicks it. I used a Texture Packing program, as part of this tutorial: https://www.youtube.com/watch?v=YPxd_xbnIpk
One of the numerous solutions I've been using is as follows:
MenuState Class-
Private skin = new Skin;
TextureAtlas buttonAtlas = new TextureAtlas ("mutebuttons.pack");
skin.addRegions(buttonAtlas);
TextButtonStyle buttonStyle = new TextButtonStyle();
ImageTextButtonStyle buttonStyle2 = new ImageTextButtonStyle();
buttonStyle.up = skin.getDrawable("soundIconON");
buttonStyle.down = skin.getDrawable("soundIconOFF");
muteButtons.pack contains the on and off mic image already (soundIconON/OFF).
Let me know if you require any more info, and thanks ahead of time.
Ed
Buttons check them selfs automatically. You don't need to do anything special and can always get the checked state by .isChecked. A button starts by default unchecked but you can change the button to checked when you create it by setChecked(true).
If you want to change the look of a button depending on it's checked state all you have to do is add the drawable. Internally it does something like If no checked state image just use default image.
Now for the type of button I think you will be fine with just a image button but any of the buttons can create a button with changeable looks. The ImageButton just allows you to add a image over the background of the button. These are the styles for the 3 different buttons:
TextButton.TextButtonStyle textButtonStyle =
new TextButton.TextButtonStyle(
Drawable up, //Background for up state
Drawable down, //background for down state
Drawable checked, //background for checked == true
BitmapFont font); //font
ImageButton.ImageButtonStyle imageButtonStyle =
new ImageButton.ImageButtonStyle(
Drawable up,
Drawable down,
Drawable checked,
Drawable imageUp, //Image for up state
Drawable imageDown, //Image for down state
Drawable imageChecked, //Image for checked == true
BitmapFont font);
//Never actually used this one, you made me aware of it's excistance. I think this is a redundant class.
ImageTextButton.ImageTextButtonStyle imageTextButtonStyle =
new ImageTextButton.ImageTextButtonStyle(
Drawable up,
Drawable down,
Drawable checked,
BitmapFont font);
So all you need to do is set the checked drawable and start clicking the button.
You can set these in a skin files as well, just call the proper drawable names from json. This is how I create a simple toggle button using text button.
com.badlogic.gdx.scenes.scene2d.ui.TextButton$TextButtonStyle:
{
default: { up: my_up_button, checked: my_checked_button, font: default-font, fontColor: white }
}
If I don't want text over it just hand a empty string. Alternatively you can alter the text on a click as well.
final TextButton button = new TextButton("Ewwww! Touch me!", skin);
button.addListener(new ClickListener() {
#Override
public void clicked(InputEvent event, float x, float y) {
super.clicked(event, x, y);
if (button.isChecked())
{
button.setText("Now I am unchecked!");
}
else
{
button.setText("Now I am checked!");
}
}
});
Let suppose we have the custom ListView which extends onDraw() method by drawing some rectangle in it.
class Listpicker extends ListView {
//..Some other methods here..//
#Override
protected void onDraw(android.graphics.Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(glassPickerBitmap, null, glassRect, semiTransparrentPaint);
}
}
The bitmap drawn represent some sort of glass which is rectangle with width equal to the width of listview and some height. What I want is when list item scrolls into this rectangle the color used for drawing text (not all item with background and etc.) would be changed into some another color. So, for example, when list item fits so that only half height of text fits into glassPickerBitmap, the outside part of list item should remain in its ordinal colors, and the part that is inside glassPickerBitmap should change its text color. List items are simple TextViews without any background.
How can be this achieved? Maybe any ColorFilters assigned to the Paint? But where? onDraw() method of Listview is not even called when ListView is scrolled... Can be this done inside customized views, for example, that will be ListView's items? Or may be some Color/Shader/do not know what else overlay can be used here, but where?
EDIT (added image examples):
Here is example of some crop from real-life app. There are 2 ListViews: one on the left side, other on the right. Glass is the grayed rectangle. Now, left list has "United States Dollar" currency selected. And I am scrolling right ListView in that way, that selection is somewhere between USD and Afghan Afghani. Now, what I want, is that USD currency in the left ListView would be drawn in red (exact color doesn't matter now, I will change it later to something meaningful) AND, in the same time, bottom part of "United States Dollar" and top part of "Afghan Afghani" in the right ListView would be drawn also in the same red color.
This color changing should be done in dynamic way - color should be changed only for the part of text that is under glass rectangle during scrolling of the list.
*OK, EUR and USD here are special currencies drawn with not standard cyan color. The question is about at least text with white color. But if it will be possible to change also cyan color it would be great.
As you can tell in their implementation they use and update a fixed position in their ListAdapter so then they update the View that matches accordingly, which is to me the easiest and simpler way to do it, but you don't get the half-half colored text, which I think you still want :)
So here is probably one of the worst answers I've given, sorry about it, I'll probably revisit this or hope someone can point you to a better solution
You can see a blurry demo at:
http://telly.com/Y98DS5
The dirty way:
Create a Paint with a proper ColorMatrixColorFilter, for the purposes of answering this I'm just desaturating, you'll have to figure out your 5x4 matrix for ColorMatrix to get the cyan your looking for. Also you will need some Rects to define source and dest when drawing below.
Create an off screen Bitmap and Canvas which you'll use to draw your list, that's done in onSizeChanged so we have proper view layout values, here you can start noticing the dirtiness as you will have to allocate a Bitmap as big as your ListView so you may get OutOfMemoryException.
Override draw to use your off screen Canvas and let ListView draw to it, then you can draw the Bitmap to the given Canvas that will draw your list as usual, then draw just the part of that Bitmap which will be drawn differently using the Paint we defined before, you will get overdraw for those pixels.
Finally draw your pre-fabricated glass Bitmap, I would recommend a n-patch (9-patch) so it scales and looks sharp across screens and densities, then you get more overdraw for those pixels.
The whole cake looks like:
public class OverlayView extends ListView {
private RectF mRect;
private Rect mSrcRect;
private Paint mPaint;
private Canvas mCanvas;
private Bitmap mBitmap;
private float mY;
private float mItemHeight;
public OverlayView(Context context) {
super(context);
initOverlay();
}
public OverlayView(Context context, AttributeSet attrs) {
super(context, attrs);
initOverlay();
}
public OverlayView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initOverlay();
}
private void initOverlay() {
// DO NOT DO THIS use attrs
final Resources res = getResources();
mY = res.getDimension(R.dimen.overlay_y);
mItemHeight = res.getDimension(R.dimen.item_height);
//
mRect = new RectF();
mSrcRect = new Rect();
mPaint = new Paint();
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.setSaturation(0);
mPaint.setColorFilter( new ColorMatrixColorFilter(colorMatrix) );
}
#Override
public void draw(Canvas canvas) {
super.draw(mCanvas);
canvas.drawBitmap(mBitmap, 0, 0, null);
canvas.drawBitmap(mBitmap, mSrcRect, mRect, mPaint);
}
#Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mRect.set(0, mY, w, mY + mItemHeight);
mRect.roundOut(mSrcRect);
mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
}
}
So that said, I hope you end up with a cleaner solution, even if you don't get that half-half sexiness :)
First, keep a spare bitmap and canvas:
class Listpicker extends ListView {
Bitmap bitmapForOnDraw = null;
Canvas canvasForOnDraw = null;
Make sure it's the right size:
#override
protected void onSizeChanged (int w, int h, int oldw, int oldh) {
if (bitmapForOnDraw != null) bitmapForOnDraw.recycle();
bitmapForOnDraw = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
canvasForOnDraw = new Canvas(bitmapForOnDraw);
}
And when we come to drawing the list:
#override
protected void onDraw(android.graphics.Canvas realCanvas) {
// Put the list in place
super.onDraw(realCanvas);
// Now get the same again
canvasForOnDraw.drawColor(0x00000000, PorterDuff.Mode.CLEAR);
super.onDraw(canvasForOnDraw);
// But now change the colour of the white text
canvasForOnDraw.drawColor(0xffff00ff, PorterDuff.Mode.MULTIPLY);
// Copy the different colour text into the right place
canvas.drawBitmap(bitmapForOnDraw, glassRect, glassRect overwritePaint);
// finally, put the box on.
canvas.drawBitmap(glassPickerBitmap, null, glassRect, semiTransparrentPaint);
}