I've customized textview but its just rotating its text not its whole view. and I don't want to use animation because I'm dragging textveiw as well so moving animated textview shows weird result, so sticking to onDraw/draw, plus overriding draw is nothing doing special.
#Override
protected void onDraw(Canvas canvas) {
// TO-DO Auto-generated method stub
canvas.save();
canvas.rotate(rotationAngle, rotationW, rotationH);
super.onDraw(canvas);
canvas.restore();
}
Same problem Rotate TextView without cut off text?
how can I rotate the whole view along text??
If you want to rotate the view use View.setRotation().
Related
I made a code where a circle moves around, but whenever it moves in front of a textview, the textview gets in front of him and I want him to be in front of the textview.
I tried drawing the circle after making the textview but it doesn't fix it.
Example:
public MainView(Context context) {
super(context);
}
public void onDraw(Canvas canvas){
((ViewGroup) text.getParent()).removeView(text);//the text was already added to the activity
Paint paint=new Paint();
paint.setColor(Color.WHITE);
canvas.drawCircle(0.0,0.0,500.0, paint);
main.addContentView(text, parameters);//adding a textview named text in the activity
invalidate();
}
Don't create Views inside onDraw, it's called so many times and you will have a lot of TextViews but you only need one, normally created on activity onCreate.
The TextView seems in front due the children is clipped when drawing, try setting clipChildren to false in the ViewGroup container
I hava a customized view and I draw its UI on its onDraw(Canvas canvas) method. I some case I need do some animation (anim is true)
public class GameView extends View {
//more code
#Override
protected void onDraw(Canvas canvas)
{
canvas.drawBitmap(item.getBitmap(), item.getXY().getX(), item.getXY().getY(), null);
canvas.drawBitmap(ResizedBitmapMapping.getHouse(), 0f, 0f, null);
if(amin){
canvas.save();
canvas.rotate(currentValue);
drawBall(canvas);
canvas.restore();
}
}
But the ball is very small, so only a small part of the view needs to be re-drawn. it should be a performance issue to draw the whole view. What is the right to draw such animation?
What your are looking for is Canvas.clipRect().
Here is a short video from Google that explains how it works and how to use it. Alternatively, you can invalidate only a region of a view with View.invalidate(int,int,int,int).
I've customized imageview but its just rotating its bitmap not the whole view. I don't want to use animation because I'm dragging imageview as well so moving animated imageivew results weird, so sticking to onDraw/draw, plus overriding draw is nothing doing special.
#Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
canvas.save();
canvas.rotate(rotationAngle, rotationW, rotationH);
super.onDraw(canvas);
canvas.restore();
}
how can I rotate the whole view not only its bitmap??
I did not get the provided answers intend here, but using
android:clipChildren="false"
for the parent ViewGroup
and overriding draw instead of onDraw did the job for me
For further information see:
Rotating imageview and its background without cropp
One must check width and height of the imageview, if Height > width then at the time of rotation width must be equal to height and same with height.
Make width and height equal to fill_parent and then rotate/translate/scale via matrix.
Starting with API 11, there is the following method available for each view:
.setRotation(float angle);
http://developer.android.com/reference/android/view/View.html#attr_android:rotation
Before API 11 you could add a rotate to the main-element of your view:
#Override
protected void onDraw(Canvas c) {
c.save();
c.rotate(...);
super.onDraw(c);
c.restore();
}
i have an imageview in my xml file and i want to put another image over it where user clicks.
i have used a canvas to place an image where user clicks but i am stuck when it comes to putting the canvas over my imageview.
Or is there any way around when we want to put another image over an imageview while detecting user clicks?
Thanks
Since you're already overriding a View method, I would say have that draw the image rather than an ImageView. Your onDraw() method would essentially be:
#Override
public void onDraw(Canvas canvas) {
canvas.drawBitmap(bottomBitmap, xCorner, yCorner, paintObject);
canvas.drawBitmap(topImage, fingerXCorner, fingerYCorner, paintObject);
}
EDIT:
Alternatively, if you want to keep the ImageView because of the convenient scaletypes, then you can override a FrameLayout. Place the ImageView inside of it, then override the draw() method to draw a bitmap.
#Override
public void draw(Canvas canvas) {
super.draw(canvas); // draws everything in the FrameLayout
canvas.drawBitmap(topImage, fingerXCorner, fingerYCorner, paintObject);
}
Have you tried using an ImageButton?
<ImageButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:onClick="onButtonClick"
android:src="#drawable/yourImageFile" />
Then define your onButtonClick method in your Activity,
public void onButtonClick(View v) {
/* Do something */
}
I am using the onDispatchDraw(Canvas canvas) method to draw lines in my view. When I call canvas.drawLine() it always draws the line on top of all my views. Is there any way to draw the line under a button but on top of another view in my layout using canvas.drawLine()?
I have tried the following but it still draws the line over the button.
Button b;
RelativeLayout r;
#Override
protected void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
Paint p = new Paint();
p.setColor(Color.Black);
canvas.drawLine(0,0,100,100,p);
r.removeView(b);
r.addView(b);
}
You're trying to reinvent the wheel. Z-ordering is already implemented in the window management subsystem and you can use it.
Create a custom view you want to draw on.
Make it non-clickable using android:clickable="false" or setClickable(false).
Make its background transparent and implement dispatchDraw().
Put all the views you don't want to draw on above this view in the view hierarchy.
Call super.dispatchDraw() after drawing the line. The dispatchDraw is called by viewgroup for drawing its children, so in your case, calling super.dispatchDraw() will draw the button first then you are drawing the line over it. Do dispatchDraw this way :
Updated code
class myListViewWithLine extends ListView {
....
#Override
protected void dispatchDraw(Canvas canvas) {
Paint p = new Paint();
p.setColor(Color.Black);
canvas.drawLine(0,0,100,100,p);
super.dispatchDraw(canvas);
}
....
}
Another method will be to just add a view with 1 dip as height and background color whatever you like under the button. It should look like like a line.
View v = new View(this);
ViewGroup.LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT,1);
v.setLayoutParams(lp);
v.setBackgroundColor(Color.WHITE);
addView(v,INDEX NUMBER AFTER THE BUTTON);