Is there another way of rotating an EditText apart from using the RotateAnimation class?
Yes. You have to write your own EditText and override the onDraw:
#Override
protected void onDraw(Canvas canvas) {
canvas.save();
canvas.rotate();
canvas.restore();
}
of course the TextPaint it is on your own too.
Related
I have a custom view extends form TextView,and write some code in onDraw():
#Override
protected void onDraw(Canvas canvas) {
if (mTextShader == null) {
createShader();
}
shaderMatrix.setTranslate(mProgressWidth,0);
mTextShader.setLocalMatrix(shaderMatrix);
Paint paint = new Paint();
paint.setShader(mBgShader);
canvas.drawRect(0,0,mProgressWidth,getBottom(),paint);
getPaint().setShader(mTextShader);
super.onDraw(canvas);
}
It looks like this:
enter image description here
Just set some shader,and draw a rect,it works fine if I don't use
android:gravity="center"
and
android:singleLine="true"
together,(just one of them is pretty good,)
if only use this property,these shaders and rect are gone!
why?
Before you draw in the canvas, translate the scrolled XY distance
canvas.translate(getScrollX(), getScrollY());
canvas.drawRect(0,0, getWidth(), getHeight(), borderPaint);
your super.onDraw(canvas); should be first line otherwise parent will paint on top of your drawing. also call this setWillNotDraw(false) in your constructor.
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.
Is it possible to rotate an ImageButton using the onCreate() function? Or do you have to use an Animation which starts onCreate()? Because with an Animation i can see a little "flick" on Activity start...
you can use ViewCompat.setRotation(buttonInstance, rotationAngle);. From the documentation
Sets the degrees that the view is rotated around the pivot point.
You can override the onDraw() method using a custom class that extends ImageButton (which I'm sure you have).
#Override
protected void onDraw(#NonNull Canvas canvas) {
super.onDraw(canvas);
// Rotate a Bitmap
final Matrix matrix = new Matrix();
matrix.setRotate(angle, imageCenterX, imageCenterY);
canvas.drawBitmap(bitmap, matrix, null);
/*
* OR
**/
// Rotate the canvas
canvas.save(Canvas.MATRIX_SAVE_FLAG);
canvas.rotate(-angle);
canvas.drawBitmap(bitmap, left, top, null);
canvas.restore();
}
Choose one or the other solution, not both together ;)
EDIT
After some quick reflection, that could also work (not tested):
#Override
protected void onDraw(#NonNull Canvas canvas) {
canvas.rotate(-angle);
super.onDraw(canvas);
}
Hi friends I want to make the squares in a row but I could not. My code is below. Can you help me?
int x=10,y=10;
#Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
Paint p= new Paint();
for(int i=1;i<6;i++)
{
canvas.drawBitmap(kutu, x,y, p);
x +=50;
}
invalidate();
}
Thank you for helping.
Two issues:
You need to reset x at the start of your onDraw method (Ideally, x should be a local variable, not a field)
Remove the call to invalidate
I'm looking to transform an image using a Matrix on the onDraw method of a custom class I created which extends ImageView e.g.,
#Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
canvas.setMatrix(imageMatrix);
canvas.drawBitmap(((BitmapDrawable)mIcon).getBitmap(), imageMatrix, null);
canvas.restore();
}
However, what I coded above does not really work. How exactly do I apply the imageMatrix on the canvas? Thanks!
Try calling Drawable.draw(Canvas) method:
#Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
canvas.setMatrix(imageMatrix);
((BitmapDrawable)mIcon).draw(canvas);
canvas.restore();
}
All you did is good, just put the super call to be the last, coz there is where all the painting is done...
#Override
public void onDraw(Canvas canvas) {
canvas.setMatrix(imageMatrix);
super.onDraw(canvas);
}