Circular progress bar in android which rotate in certain defined pattern [duplicate] - android

I am beginner to Animation in Android.
I want to set animation like Pendulum(Swing left to right) to image in my Activity.
what I have done so far is:
Animation anim = new RotateAnimation(0, 30, 0, 0);
anim.setRepeatMode(Animation.REVERSE);
anim.setInterpolator(new AccelerateDecelerateInterpolator());
anim.setDuration(1500);
anim.setFillAfter(true);
But it doesn't work at all...
can anybody suggest me how to do Animation?
Thank You....

I had the exact same problem, and ended up with an xml-only solution: http://blog.sqisland.com/2012/01/android-pendulum-animation.html

This will help you ::
AnimationDrawable Tranninganimation5;
Tranninganimation1 = new AnimationDrawable();
new playninzi().execute();
animation.setOneShot(false);
Tranninganimation1.setOneShot(false);
private class playninzi extends AsyncTask<Void, Void, Void> {
private final ProgressDialog dialog = new ProgressDialog(
Signs_main_page.this);
protected void onPreExecute() {
this.dialog.setMessage("Please Wait...");
this.dialog.show();
try {
for (int i = 1; i < 25; i++) {
Bitmap bitmap = BitmapFactory
.decodeStream((InputStream) new URL(
"http://203.a44.115.55/MRESC/images/test/girl2/"
+ "girl-1000" + i + ".png")
.getContent());
Drawable frame = new BitmapDrawable(bitmap);
animation.addFrame(frame, 50);
}
} catch (Exception e) {
}
#Override
protected Void doInBackground(Void... arg0) {
return null;
}
protected void onPostExecute(final Void unused) {
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
}
}
in this example i have attach image from server but you can also set it drawable and display animation

If I were you, and doing such complex animations, I would try looking into the simply API of canvas to draw and animate objects. Take a look at lunarLandar for an example.
Would be very simple, if you control the actual placement of the object, to simply create a formula that adjusts the X & Y of the object.
Sudo:
-If object is going right, and is left of center, decrease it's Y value.
-If object is going right, and is right of center, increase it's Y value.
-If the object is going left, and is right of center, decrease it's Y value.
-If the object is going left, and is left of center, increase it's Y value.

Related

Android TransitionDrawable not fading

I have an ImageView and I am trying to fade from one image to the next using this code:
Drawable bgs[] = new Drawable[2];
public void redraw(int[][] grid) {
bgs[0] = bgs[1];
bgs[1] = new GameDrawable(grid, prefs.colors);
if (bgs[0] == null) {
gameField.setImageDrawable(bgs[1]);
} else {
TransitionDrawable crossfader = new TransitionDrawable(bgs);
crossfader.setCrossFadeEnabled(true);
gameField.setImageDrawable(crossfader);
crossfader.startTransition(500);
}
}
gameField is correctly referenced as an ImageView.
gameDrawable simply extends Drawable and draws the grid.
On each move and action the new GameDrawable is being rendered correctly but there is no fading whatsoever. The new image is simply displayed instantaneously. I have tried lengthening the transition time and swapping the order of the drawables with no effect.
Any help on is appreciated.
Update: I have now set my transition to something ridiculously long like 500000. The first drawable shows for a few seconds and then suddenly the second drawable appears. So still no transition.
Update 2:
I think my Drawable might be implemented incorrectly, so I have attached the code.
public class GameDrawable extends Drawable {
private Paint paint = new Paint();
private float blockWidth = 1;
private int[][] myGrid;
private int myColor;
private List<Point> myPoints;
public GameDrawable(int[][] grid) {
super();
this.myGrid = grid;
this.myColor = colors[yourColor];
paint.setStrokeWidth(1);
paint.setStyle(Paint.Style.FILL);
paint.setAlpha(0);
this.myPoints = yourPoints;
}
#Override
public void draw(Canvas canvas) {
float height = getBounds().height();
float width = getBounds().width();
blockWidth = width / myGrid.length;
if (height / myGrid.length < blockWidth) {
blockWidth = height / myGrid.length;
}
for (int x = 0; x < myGrid.length; x++) {
for (int y = 0; y < myGrid[x].length; y++) {
paint.setColor(colors[myGrid[x][y]]);
canvas.drawRect(x * blockWidth, y * blockWidth, (x+1)*blockWidth, (y+1)*blockWidth, paint);
}
}
}
#Override
public void setAlpha(int alpha) {
paint.setAlpha(alpha);
invalidateSelf();
}
#Override
public void setColorFilter(ColorFilter cf) {
paint.setColorFilter(cf);
invalidateSelf();
}
#Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
}
}
Looking at your code, I see a problem at the line
bgs[0] = bgs[1];
bgs[1] has not yet been defined before this line and so bgs[0] is null for the first method call. Because of this, (bgs[0] == null) is true, and so the later defined bgs[1] is directly set to the gameField ImageView.
Use corrected code below.
Drawable bgs[] = new Drawable[2];
Drawable firstDrawable = getResources().getDrawable(R.drawable.transparent);
public void redraw(int[][] grid) {
bgs[0] = firstDrawable;
bgs[1] = new GameDrawable(grid, prefs.colors);
firstDrawable = bgs[1];
TransitionDrawable crossfader = new TransitionDrawable(bgs);
crossfader.setCrossFadeEnabled(true);
gameField.setImageDrawable(crossfader);
crossfader.startTransition(500);
}
Note that TransitionDrawable does not work properly when the Drawable sizes are different. So you may need to resize firstDrawable beforehand.
EXTRA: I would avoid setCrossFadeEnabled(true) since the whole TransitionDrawable becomes translucent during the transition, revealing the background. Sometimes, this creates a "blinking" effect and destroys the smoothness of the transition.
EDIT: Looking at your custom Drawable implementation, I think the problem lies in the line
canvas.drawColor(Color.WHITE);
in the draw() method.
I looked at TransitionDrawable.java source and found that setAlpha is called on the drawables to get the cross fade effect. However, your canvas has a solid white color and setAlpha() only affects the paint. Hope this is your answer.
EDIT 2: The actual problem, as pointed out by Michael, was that TransitionDrawable's setAlpha() calls on the Drawables were rendered ineffective due to paint.setColor() in the GameDrawable's draw() method overriding the paint's alpha value set by the TransitionDrawable.

9patch looks pixeled when setSize is called too many times

I have a sprite that is supposed to act like a loadbar. I have tried this by using an example image that has been created like a 9patch-type (http://cdn.dibbus.com/wp-content/uploads/2011/03/btn_black.9.png). It seems okay in the start, but as the width of the sprite increases the sprite starts to look pixeled. Anyone know what the problem could be, or have any solution? The code is shown below.
public Sprite loaded;
public void init()
{
atlas = new TextureAtlas(Gdx.files.
internal("data/misc/menu_button.pack"));
loaded = atlas.createSprite("loadbar");
loaded.setPosition((Misc.WIDTH/2) - unloaded.getWidth()/2,
Misc.HEIGTH - unloaded.getHeight());
}
public void draw_load_bar() //render function
{
if(loaded.getWidth() < 600)
{
loaded.setSize(loaded.getWidth()+ 0.5f, loaded.getHeight());
}
loaded.draw(batch);
}
Dont use a Sprite to stretch it. I'd recommend a real Ninepatch from libgdx for it.
public NinePatch loaded;
private float posX, posY, width, height;
public void init()
{
loaded = new NinePatch(the Texture here,10, 10, 10, 10); //bounds outside
//set right pos here...
}
public void draw_load_bar(SpriteBatch batch) //render function
{
if(loaded.getWidth() < 600)
{
//update the size of it here (guess pos is static)
width++;
}
//need to parse the batch and the right sizes.
loaded.draw(batch, posx, posy, width, height);
}
after that you can handle it like a Sprite or Texture but it does stratch right without issues. If you want to the full Picture to be Stretched simply do net set any bounds at the creation new NinePatch(texture, 0,0,0,0)

how to animate a Bitmap image with motion in android

Score is a drawable image
private Bitmap Scores;
private boolean running =true;
private SurfaceView mySurfaceHolder;
Scores.decodeResource(myContext.getResource(), R.drawable.score);
//this is just the run procedure
#Override
public void onRun()
{
while(running){
Canvas c = null;
try{
c = mySurfaceHolder.lockCanvas(null);
synchronize(mySurfaceHolder){
drawScore(c); //here is where i call my method..
}
}finally{
mySurfaceHolder.unlockCanvasAndPost(c);
}
}
//the pontsGain and onTitle are both boolean variables so please don't get confuse
//and pointScoreAtY is an integer that will allocate my specific position in screen
private void drawScore(Canvas c)
{
try{
if(pointsGain && !onTitle)
{
pointScoreAtY = (int)(FingerY -(Scores.getHeight()/2)-100);
int i=100;
do{
try{
c.drawBitmap(Scores, (FingerX -(Scores.getWidth()/2))+200,pointScoreAtY, null);
invalidate();
pointScoreAtY -=i;
}catch(Exception e){}
}while(i-->0);
pointsGain = false;
}
}catch(Exception e){}
}
This specific function is drawing a bitmap into the screen at current location but it is not what i am looking to do... for example my main goal is when a character on my game gets hit i want to show a scorePoint and animate it so that it can go up and disappear from screen.
You can add animation to your imageview like this:
TranslateAnimation anim = new TranslateAnimation( 0, xDest - originalPos[0] , 0, yDest - originalPos[1] );
anim.setDuration(1000);
anim.setFillAfter( true );
view.startAnimation(anim);
For more animation details check android developer site:
http://developer.android.com/training/animation/index.html

How can I make my image animation smoother?

I made a panorama app that scrolls the image automatically, from left to right to left and so on. It works nicely, except for the fact that it doesn't quite scrolls as smoothly (meaning constant velocity) as I would like. It sometimes 'jumps' a bit. It shouldn't be in a device with a core of 1.4 GHz...
I implemented this just by translating the image, then waiting a bit with a Thread.sleep(panoanimspeed); (where 1/1000s <= panoanimspeed <= 1/50s), and so on. Probably not the smartest thing to do...?
Do you know how can I make this smoother? Below is the code of the relevant AsyncTask.
Thanks!
class AnimatePanorama extends AsyncTask<Void, Integer, Void> {
private float scale = foto.getCropScale() * foto.getBaseScale();
private Matrix m = foto.matrix;
#Override
protected void onProgressUpdate(Integer... values) {
if (values[0] == 0) {
m.setScale(scale, scale);
m.postTranslate(values[1], 0);
foto.setImageMatrix(m);
}
}
#Override
protected Void doInBackground(Void... arg) {
dir = -dir;
int l, ini = 0;
float lim = foto.getImageWidth()*scale - foto.getViewWidth();
float[] mm = new float[9];
foto.matrix.getValues(mm);
if (foto.getScale() == foto.getCropScale())
ini = (int) (dir*(lim*(dir+1)/2 + mm[2]));
while (true) {
if(isCancelled()) break;
l = (int) (lim*(dir+1)/2);
for (int i = ini; i < lim; i++) {
if(isCancelled()) break;
try {
Thread.sleep(panoanimspeed);
} catch (InterruptedException e) {
e.printStackTrace();
}
publishProgress(0, dir * i - l);
}
ini = 0;
dir = -dir;
}
return null;
}
}
Create an animation by extending Animation class (http://developer.android.com/reference/android/view/animation/Animation.html)
and override the applyTransformation(float interpolatedTime, Transformation t) method.
Base your image translation on a "real time" based translation, like X pixels per second.
On the first cycle, save the system time as a base time stamp.
On future loop cycles, calculate the new location based on the time since the base time stamp.
This will avoid the unreliability of the Thread.sleep "real world" timing.

How to Swing image like pendulum in android?

I am beginner to Animation in Android.
I want to set animation like Pendulum(Swing left to right) to image in my Activity.
what I have done so far is:
Animation anim = new RotateAnimation(0, 30, 0, 0);
anim.setRepeatMode(Animation.REVERSE);
anim.setInterpolator(new AccelerateDecelerateInterpolator());
anim.setDuration(1500);
anim.setFillAfter(true);
But it doesn't work at all...
can anybody suggest me how to do Animation?
Thank You....
I had the exact same problem, and ended up with an xml-only solution: http://blog.sqisland.com/2012/01/android-pendulum-animation.html
This will help you ::
AnimationDrawable Tranninganimation5;
Tranninganimation1 = new AnimationDrawable();
new playninzi().execute();
animation.setOneShot(false);
Tranninganimation1.setOneShot(false);
private class playninzi extends AsyncTask<Void, Void, Void> {
private final ProgressDialog dialog = new ProgressDialog(
Signs_main_page.this);
protected void onPreExecute() {
this.dialog.setMessage("Please Wait...");
this.dialog.show();
try {
for (int i = 1; i < 25; i++) {
Bitmap bitmap = BitmapFactory
.decodeStream((InputStream) new URL(
"http://203.a44.115.55/MRESC/images/test/girl2/"
+ "girl-1000" + i + ".png")
.getContent());
Drawable frame = new BitmapDrawable(bitmap);
animation.addFrame(frame, 50);
}
} catch (Exception e) {
}
#Override
protected Void doInBackground(Void... arg0) {
return null;
}
protected void onPostExecute(final Void unused) {
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
}
}
in this example i have attach image from server but you can also set it drawable and display animation
If I were you, and doing such complex animations, I would try looking into the simply API of canvas to draw and animate objects. Take a look at lunarLandar for an example.
Would be very simple, if you control the actual placement of the object, to simply create a formula that adjusts the X & Y of the object.
Sudo:
-If object is going right, and is left of center, decrease it's Y value.
-If object is going right, and is right of center, increase it's Y value.
-If the object is going left, and is right of center, decrease it's Y value.
-If the object is going left, and is left of center, increase it's Y value.

Categories

Resources