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();
}
Related
I'm trying to implement a custom view that is created by some data - specifically I am trying to create a revenue projection graph via a custom view.
The way i'm doing this is by having 10 data points, and drawing lines between each data point. (and then also drawing the X and Y axis lines).
I was thinking about creating a view that encapsulates the 10 coordinates, and draw them via the canvas in onDraw(). something like this:
class RevView extends View {
private List<Point> mPoints;
public void configurePoints(float[] revenues) {
// convert revenues to points on the graph.
}
// ... constructor... etc.
protected void onDraw(Canvas canvas) {
// use canvas.drawLine to draw line between the points
}
}
but in order to do so, i think i need the width/height of the view, which isn't rendered until onDraw.
is this the right approach? or am i even supposed to pass in a Points list to the view? Or what's a better way to build this?
You should override the onMeasure() method which will be called before onDraw().
https://developer.android.com/reference/android/view/View.html#onMeasure(int,%20int)
Measure the view and its content to determine the measured width and the measured height.
You should use onMeasure() method which is called before onDraw() method, so you'll be able to get your view dimensions like this:
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = measureWidth(widthMeasureSpec);
int height = measureHeight(heightMeasureSpec);
}
You should avoid getting View dimensions directly in onDraw() method because it will be called many times a second
I'm making a custom View thats derived from an ImageViewand I control where the image has to be within this ImageView using padding.
I have set OnClickListener on my custom ImageView that resizes it:
image.setOnClickListener(new ImageView.OnClickListener(){
#Override
public void onClick(View v) {
image.resize_image();
}
});
and this is how this function is looking like
public void resize_image(){
ViewGroup.LayoutParams params = getLayoutParams();
params.height = new_heigth;
params.width = new_width;
setLayoutParams(params);
}
After this resizing is done I don't want my displayed image to change size (only the custom ImageView is changing size so I can draw an extra stuff around this image) within my custom ImageView so inside onDraw(Canvas) member function I set the new padding
class custom_ImageView extends ImageView{
//...
#Override
protected void onDraw(Canvas canvas) {
//...
setPadding(new_left_padding, new_top_padding, new_right_padding, new_bottom_padding);
//...
}
//...
}
Result is that width and heigth are changed like I wanted but my displayed image is neither in the right position or size.
Interesting this is that if I add an extra line of invalidate(); in the end of my resize_image() and I click on my custom_ImageView twice - on the 2nd click image draws itself in right size and position like I wanted.
Can anyone tell me why this is happening?
I kind of worked around this by not using ImageView at all but just drawing image where it needs to be by using Canvas.drawBitmap(Bitmap, RectF, RectF, Paint)
This way I don't have to specify images location using setPadding within onDraw, but I can do it simply by specifying position using RectF.
In my OnDraw method, I draw path into my canvas (circle with segements). All work well, now what I am not sure how to do is how can I tilt it (sort of rotation around one axis).
Basically, what I am trying to achieve is something like the tilt functionality of google map.
Here is the pseduo code:
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// I do all the drawing here
canvas.tilt(45degrees) <-----??
}
I don't think you have to create the tilt effect within the onDraw method modifying your canvas manually. Instead use setRotationX() method on the whole View. You can also define it in xml as android:rotationX="45".
Use Canvas.concat(Matrix) with a rotation matrix, i.e.:
class MyView {
private final Matrix rotate = new Matrix();
public MyView()
{
rotate.setRotate(45.0f);
}
protected void onDraw(Canvas c)
{
super.onDraw(canvas);
canvas.save(Canvas.MATRIX_SAVE_FLAG);
canvas.concat(rotate);
canvas.restore();
}
}
Note also that you should not do any memory allocations inside onDraw() as per the android linter.
We developing custom textview that zoom in/out depends on pinch gesture, and able to resize textview and scale text. But, when zoom out this view, text are cropped unintentionally.
We write a code like this.
1.Get scaled value in gesture listener and postScale in onDraw()
2.Calculate new view size by layout(int, int, int, int)
#Override
public void onDraw(Canvas canvas) {
android.graphics.Matrix m = new android.graphics.Matrix();
m.postScale(mScaleFactor, mScaleFactor);
canvas.concat(m);
layout((int) l, (int) t, (int)r , (int) b );
super.onDraw(canvas);
}
This is may not be perfect solution for you,But you can try it.
1)Detect the pinch gesture
2)calculate the Scale factor(you can do it manually or you can use ScaleDetector class)
3)Use LayoutParams to actually resize your textview.
4)however it will not scale the text size but you can do it programatically respective to your scale gesture.
If you have any question feel free to comment,I have done it before with slightly different requirement.
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().