Supose i ´ve got a walking character animation, how do i import it from android.
I know that you can do sprite with xml files (although it isnt smooth), but imnot really sure how to do it with bone animations.
Or is there a way to make some basic character animation in android?
If it helps im using Unity.
I would suggest you to use a video in android. For example you can use powtoon and make custom animations and presentations as a video and use it in your android project.It will solve the purpose and will be easy for you.Hope this helps :)
EDIT:
Code for image animation in android
package com.abc.def;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.ImageView;
public class Anim extends Activity {
AnimationDrawable rocketAnimation;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_anim);
ImageView rocketImage = (ImageView) findViewById(R.id.imageView1);
rocketImage.setBackgroundResource(R.drawable.anim);
rocketAnimation = (AnimationDrawable) rocketImage.getBackground();
}
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
rocketAnimation.start();
return true;
}
return super.onTouchEvent(event);
}
}
Related
I'm trying to animate an ImageView using the ValueAnimator class. However, the official documentation for the ValueAnimator class does not contain enough details(at least, for beginners). I want the ImageView to move rightwards(translate animation) upon clicking a Button. I'm using the following code but without use.
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.Button;
import android.widget.ImageView;
import android.animation.ValueAnimator;
public class TestActivity extends ActionBarActivity {
private ImageView image;
private Button animateButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
image = (ImageView) findViewById(R.id.imageView);
animateButton = (Button) findViewById(R.id.button);
final ValueAnimator animator = ValueAnimator.ofInt(0, 100);
animator.setTarget(android);
animateButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
animator.start();
}
});
}
}
When I click on the "Animate" button, no animation takes place and the image remains as it is. I just can't figure out how to get this is thing working. Any help regarding this issue will be highly appreciated.
Thanks in advance.
However, the official documentation for the ValueAnimator class does not contain enough details(at least, for beginners).
The key bit that you may have missed is:
[Using a plain ValueAnimator], however, has no real effect on an object, because the ValueAnimator does not operate on objects or properties directly.
Change your ValueAnimator to a property animator:
ObjectAnimator animator = ObjectAnimator.ofFloat(image, "x", 0f, 100f);
and drop the setTarget() line.
Or, use the simpler syntax:
image.animate().x(100f);
Based on the answer to my previous question, I'm making a heavily-customized scrolling list, and I wish to capture the child view offset (letting the basic Android functionality handle flicking and bouncing on overscroll etc).
The offset however is always returning 0. Ideas greatly appriciated
package com.example.svexample
import android.annotation.TargetApi;
import android.app.ActionBar.LayoutParams;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Build;
import android.util.Log;
import android.view.View;
import android.widget.ScrollView;
public class ScrollViewTest {
private static final String TAG = ScrollViewTest.class.getSimpleName();
private ScrollView sv;
private View v;
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
public ScrollViewTest(Context context) {
v = new View(context);
v.setBackgroundColor(Color.BLUE);
v.setBottom(800); v.setRight(400);
sv = new ScrollView(context);
sv.setBackgroundColor(Color.RED);
sv.setBottom(400); sv.setRight(400);
//The next two lines don't seem to help...
//LayoutParams lp = new LayoutParams(400, 600);
//sv.setLayoutParams(lp);
sv.addView(v);
}
public void draw(Canvas canvas){
sv.draw(canvas);
v.draw(canvas);
Log.d(TAG, "Scroll is: " + sv.getScrollY());
}
}
Turns out I needed to feed my app onTouchEvent to the ScrollView in order for it to be interactive (since it was created programatically). I would have assumed the Android UI elements do this automatically but in this case apparently not.
This question already has an answer here:
image is playing an animation and the image is clickable
(1 answer)
Closed 10 years ago.
I've made a simple animation for an image and I set the event OnClick on the image to make a toast. The problem is that I made the image started doing the animation on the onCreate and I made set the image to be clicked and fire the toast but the problem is that the image isn't clickable, but if I press on the original position of the image, the toast is started (the image is not moving with the animation)
thx for your help
this is the animation code in anim folder (translate.xml)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/linear_interpolator">
<translate
android:fromXDelta="-80%p"
android:toXDelta="80%p"
android:duration="20000"
android:repeatCount="100"
android:repeatMode="restart"
/>
</set>
and this is the Activity Class
package com.example.animatest;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
private ImageView image01;
private long aefe;
private ImageView image1;
private ImageView image2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image01 = (ImageView) findViewById(R.id.imageView1);
final Animation animTranslate1 = AnimationUtils.loadAnimation(this,
R.anim.translate);
image01.startAnimation(animTranslate1);
image01.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "hello", Toast.LENGTH_SHORT)
.show();
}
});
}
}
During the entire animation, your view remains at the old location (location when the animation just started). It is just drawn in another spot. You'd have to move your animated view after your animation ends:
Register a listener to your animation.
http://developer.android.com/reference/android/view/animation/Animation.AnimationListener.html
In your onAnimationEnd implementation, modify your Activity's layout so that it resembles the final state/layout of your animation.
Update after your comment:
The only way I see of doing this is by creating your own custom Animation in Java code and implementing your custom Animation's 'protected void applyTransformation(float interpolatedTime, Transformation t)' method. For example, in our app we have an animation that actually moves a View around instead of just drawing it at a different location. E.g. below is an example of an Animation that increases or decreases the actual height of a View:
public class ViewHeightAnimation extends Animation {
private final View view;
private final float diffHeight;
private final int startHeight;
public ViewHeightAnimation(View view, float diffHeight, int startHeight) {
this.view = view;
this.diffHeight = diffHeight;
this.startHeight = startHeight;
setDuration(200);
setInterpolator(new AccelerateDecelerateInterpolator());
}
#Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
android.view.ViewGroup.MarginLayoutParams layoutParams = (android.view.ViewGroup.MarginLayoutParams)view.getLayoutParams();
layoutParams.height = Math.round(startHeight + (diffHeight * interpolatedTime));
view.setLayoutParams(layoutParams);
}
#Override
public boolean willChangeBounds() {
return true;
}
}
Your Animation would be different, but would be using the 'getLayoutParams()' and 'setLayoutParams()' as well to modify the View's (ImageView's) position and change layoutParams.topMargin and layoutParams.leftMargin appropriately.
If you are not concerned about Android 2.x or lower, using the ObjectAnimator (3.0 or higher) or ViewPropertyAnimator (3.1 or higher) is a better solution, as was mentioned in other answer earlier.
Let me know if this helps you.
try like this:
final Animation animTranslate1 = AnimationUtils.loadAnimation(this,R.anim.translate);
animTranslate1.setFillAfter(true);
image01.startAnimation(animTranslate1);
If that does not work then you'll have to use the newer Property Animation framework (which was pointed out in the answer to your previous duplicate question)
See here to learn about it
The way you animate your imageView is only move its appearance, so actually your imageView is still at the old position. There's not an easy way to do what you want, with this type animations. You should consider to use ObjectAnimators ...
I'm stucked in this for almost a month. I have a perfect chart in my android code using 'afreechart', however, the library does not seem to have support for exporting the chart as an image yet (since it's based on 'jfreechart' and this one does the job).
I tried to get the view of the chart, convert it into canvas and save using the compress functions of the bitmap library, however everytime i try this i get a totally black image as a result. I tried this same method and it works for the other views of my code (simple views, like linearlayout and relativelayout).
After that i tried to create a routine to make a screenshot of the chart activity and close that activity after that. But I couldn't find a way to do that by code, the closest i got was with monkeyrunner.
So i gave up of this idea and tried to look for other libraries, such as 'kichart', 'achartengine', etc, but none of them seem to do the job for, i'm freaking out and i thought that exporting the chart to an image wouldn't be that hard... any ideas?
When i set a background color for the layout, the returned image is a full rectangle with the color of the background, so it's getting the layout, just not the chart.
My Code:
package com.kichart;
import java.io.File;
import java.io.FileOutputStream;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
public class Main extends Activity {
LinearLayout ll;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
float[] values = new float[] { 2.0f,1.5f, 2.5f, 1.0f , 3.0f };
String[] verlabels = new String[] { "great", "ok", "bad" };
String[] horlabels = new String[] { "today", "tomorrow", "next week", "next month" };
GraphView graphView = new GraphView(this, values, "GraphViewDemo",horlabels, verlabels, GraphView.BAR);
ll = new LinearLayout(this);
ll.addView(graphView);
Draw2d d = new Draw2d(this);
setContentView(d);
//setContentView(graphView);
}
public class Draw2d extends View {
public Draw2d(Context context) {
super(context);
setDrawingCacheEnabled(true);
}
#Override
protected void onDraw(Canvas c) {
ll.setBackgroundColor(Color.WHITE);
ll.measure(MeasureSpec.getSize(ll.getWidth()), MeasureSpec.getSize(ll.getHeight()));
ll.layout(400, 400, 400, 400);
ll.draw(c);
try {
getDrawingCache().compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(new File("/mnt/sdcard/graph2.png")));
} catch (Exception e) {
Log.e("Error--------->", e.toString());
}
super.onDraw(c);
}
}
}
You can get a image bitmap from a view by doing:
Bitmap bitmap;
chart.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(chart.getDrawingCache());
chart.setDrawingCacheEnabled(false);
if you can't call getDrawingCache on the chart, try to place it inside a layout like RelativeLayout or something like that. and call it on the layout.
After that you can save it as a image..
If the image is still black, you need to make sure the chart is created before getting the bitmap.
Hope this will help you
I'm trying to develop an android application that is basically for drawing shapes.I want the user to make gesture on the screen and the shape that is more closely matching to gesture should be drawn on the screen.
In my application,I can detect the gesture that is being performed on the screen like circle,line,rectangle etc. but there is some problem with my code.It actually detects the gesture and draws the respective shape but it happens only once.
For example. If I draw Line on the screen then line is drawn on my view but after that If I draw circle or rectangle etc then gesture is recognized but the shape is not drawn there.
Here is the full code of that
package com.pck.ShapeMaker;
import java.util.ArrayList;
import android.app.Activity;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.Prediction;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.Toast;
public class GestureDetection extends Activity {
private GestureLibrary gLib;
private LinearLayout l1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gesture);
l1 = (LinearLayout) findViewById(R.id.playArea);
gLib = GestureLibraries.fromRawResource(this, R.raw.gestures);
if (!gLib.load())
finish();
GestureOverlayView gesturesView = (GestureOverlayView) findViewById(R.id.gestures);
myGestureHandler handler = new myGestureHandler();
gesturesView.addOnGesturePerformedListener(handler);
}
class myGestureHandler implements OnGesturePerformedListener{
public void onGesturePerformed(GestureOverlayView gestureView, Gesture gesture){
ArrayList<Prediction> predictions = gLib.recognize(gesture);
if (predictions.size() > 0 && predictions.get(0).score > 1.0) {
String action = predictions.get(0).name;
if ("l".equals(action)) {
Line line = new Line(getApplicationContext(),20,230,200,230);
l1.addView(line);
} else if ("r".equals(action)) {
Rectangle rect = new Rectangle(getApplicationContext());
l1.addView(rect);
Toast.makeText(getApplicationContext(), "rect", Toast.LENGTH_SHORT).show();
} else if ("t".equals(action)) {
Triangle tri = new Triangle(getApplicationContext(), 300,300,250, 350, 350, 350);
l1.addView(tri);
Toast.makeText(getApplicationContext(), "trianlge", Toast.LENGTH_SHORT).show();
}else if ("c".equals(action)) {
Circle c1 = new Circle(getApplicationContext(),50,50,30);
l1.addView(c1);
Toast.makeText(getApplicationContext(), "circle", Toast.LENGTH_SHORT).show();
}else if ("d".equals(action)) {
Toast.makeText(getApplicationContext(), "diamond", Toast.LENGTH_SHORT).show();
}
}
}
}
}
It looks like your code will produce an ever-increasing number of views within the LinearLayout. Is that what you intended? If not, you could try removing the obsolete views from the layout before adding the new one. The problem may be that there is no space in the layout for subsequent views after the first one is added.
Also, dynamically adding views like this, seems like an odd way to draw graphics. Why not define a single custom view to cover all types of drawing and include that statically in XML? Your onGesturePerformed() method would then call some method of your view to specify the type of drawing to be performed, then call invalidate() to trigger redrawing. The onDraw() method of your view would then draw whichever graphic was just specified.