Can i use objectAnimator in xml in api level 2.2 for fragment transition to to get more types of animation?? or provide me xml for animations like glide, cube, stack etc provided with API 11 and above wich supports API 8 too.
private void transitionFade() {
mFragmentTransaction.setCustomAnimations(android.R.anim.fade_in,android.R.anim.fade_out,android.R.anim.fade_in,android.R.anim.fade_out);
}
The above method with "anim" works fine in API8
but below code doesn't work with "animator"
private void transitionGlide() {
mFragmentTransaction.setCustomAnimations(R.animator.glide_fragment_horizontal_in, R.animator.glide_fragment_horizontal_out, R.animator.glide_fragment_horizontal_in, R.animator.glide_fragment_horizontal_out);
}
No, you cannot directly, as ObjectAnimator appeared in API11, however you can try to use backport of Honeycomb Animation system NineOldAndroids
Related
After testing my app on Android 5.0, I noticed that image.setAlpha() is not working on this Android version.
I tried with image.setImagealpha() function, but it returns this error:
"The method setImageAlpha(int) is undefined for the type Drawable"
The API level that I´m using on my app is 8
What can I do?
Update 2019:
With kotlin now we can do it like this:
imageView.post {imageView.alpha = 1.0f}
I have used View.post so that it get updated on immediate next UI updation cycle.
Without posting on UI thread sometimes setting alpha of TextViews doesn't reflect on UI as discussed here
ImageView has the method setAlpha(float) after API 11. Before API 11 it uses setAlpha(int). Since you want to support API 8 and above, you have to specify the different states. So to resolve this, use the following code:
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB){
//For API 11 and above use a float such as 0.54.(54% transparency)
imageView.setAlpha(float);
}
else
//For API Below 11 use an int such as 54.
imageView.setAlpha(int);
I want to use the LayoutTransition class that it can achieve animation.But Eclipse tell me Call requires API level 11 (current min is 7). And I just want to call this API in Android2.1+. So, Here What way can be deal with it(such as the Open Sources library)?
Thank you very much for your answer.
This error comes from Android Lint. You can suppress it by adding an
#SuppressLint("NewApi"). Then in the body of the method using LayoutTransition you can add the following code:
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
//use LayoutTransition in your code
} else {
//on Android 7-11, don't use LayoutTransition
}
I have a button that I want to set the background of using a png file from internal storage. For android api 16 and up, this works fine:
filePath = getActivity().getFileStreamPath(colorCodes.get(i-1));
temp.setBackground(Drawable.createFromPath(filePath.toString()));
When running on an android tablet with 4.0.4, this part crashes the app with a nosuchmethod error (setBackground). After a little research, I see that setBackground is only available for api 16+. After looking around on SO and a few other places, it looks like I need to use setBackgroundDrawable (deprecated) or setBackgroundResource. I tried this:
filePath = getActivity().getFileStreamPath(colorCodes.get(i-1));
if (android.os.Build.VERSION.SDK_INT < 16) {
temp.setBackgroundDrawable(Drawable.createFromPath(filePath.toString()));
} else {
temp.setBackground(Drawable.createFromPath(filePath.toString()));
}
When logging it out, it shows that setBackgroundDrawable is running and not setBackground, but I get the same nosuchmethod error (setBackground).
The other option is setBackgroundResource, but it accepts an int and not a drawable. Can I convert from drawable to int for this purpose?
What can I do here to set the background of the button to a file in internal storage for APIs < 16?
Thanks.
***EDIT - ok, this is working. just missed a little part elsewhere in the code that had the same problem. However, is using a deprecated method really the only way?
Deprecation is a status applied to a computer software feature,
characteristic, or practice indicating it should be avoided, typically
because of it being superseded. The term is also sometimes used for a
feature, design, or practice that is permitted but no longer
recommended in other areas, such as hardware design or compliance to
building codes. (source link)
Now we can answer your question.
Before API level 16 there is a method named setBackgroundDrawable. After API Level 16 google decided to write a new method setBackground for same purpose and recommend us to use new method. (Reason of this may be found by googling.)
You can use setBackgroundDrawable method for all api levels. There aren't any constraint for this. But using new method setBackground is recommended after API Level 16.
But you can only use setBackground method for devices which is running on API Level 16 or higher. So if you only implement setBackground method in your code, you are going to get MethodNotFoundException for devices which run below API Level 16.
To sum up; it is a best practice(for me it is a must) to use new methods then deprecated ones with supportted api version check such as;
if (android.os.Build.VERSION.SDK_INT < 16) {
temp.setBackgroundDrawable(Drawable.createFromPath(filePath.toString()));
} else {
temp.setBackground(Drawable.createFromPath(filePath.toString()));
}
I am not quite sure whether it is the only way to achieve this but in my opinion it is the correct one. Because the annotation #Deprecated defines the method to be superseded (in most cases) it automatically implies you can (I would even say should) use it to address older versions which are the targeted versions of this method.
how can I set an xml background file that placed in drawable for a view without using #SuppressLint("NewApi") ?
for example I created a drawable xml file for my textview
when I call TV.setBackground(getResources().getDrawable(R.drawable.tv_pic_back)); eclipse automatically add #SuppressLint("NewApi") at the first of my function.
how can I use that without #SuppressLint("NewApi") ?
I have a class where I put a lot of code to handle the different APIs, so that you use one line of code for one API, and another line of code for another API.
public static void setBackgroundDrawable(View view, Drawable drawable) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
view.setBackground(drawable);
}
else {
view.setBackgroundDrawable(drawable);
}
}
This will still give you a warning because setBackgroundDrawable is deprecated, but if you instead would use setBackground(drawable) for all versions then your application would crash on API levels lower than Jelly Bean (API 16).
However, in your case all you need to do is actually setBackgroundResource(R.drawable.tv_pic_back); because you don't need to get the drawable from the resource id yourself, Android will do that for you if you give it your resource id when you call the right method.
The Android developer reference will tell you which methods are deprecated and which methods are implemented in which API version.
problem in this code ?
its work on android 4 successfully ! but don't work on 2.2 !
i use nineoldandroids library for android API 8
if(Build.VERSION.SDK_INT > 13) {
v.setTranslationX(0.0F);
v.setTranslationY(height);
v.setRotationX(45.0F);
v.setScaleX(0.7F);
v.setScaleY(0.55F);
ViewPropertyAnimator localViewPropertyAnimator =
v.animate().rotationX(0.0F).rotationY(0.0F).translationX(0).translationY(0).setDuration(animDuration).scaleX(
1.0F).scaleY(1.0F).setInterpolator(interpolator);
localViewPropertyAnimator.setStartDelay(0).start();
} else {
com.nineoldandroids.view.ViewPropertyAnimator.animate(v).translationX(0.0F).translationY(height)
.rotationX(45.0F).scaleX(0.7F).scaleY(0.55F);
com.nineoldandroids.view.ViewPropertyAnimator.animate(v).setStartDelay(0).start();
com.nineoldandroids.view.ViewPropertyAnimator.animate(v).rotationX(0.0F).rotationY(0.0F).translationX(0).translationY(0).setDuration(animDuration).scaleX(
1.0F).scaleY(1.0F).setInterpolator(interpolator);
com.nineoldandroids.view.ViewPropertyAnimator.animate(v).setStartDelay(animDuration).start();
}
sorry for bad english !
tnx to all
-------------------------------EDIT-----------------------------
code executed right on android 2.2 but not like android 4 !
ViewPropertyAnimator is for api level >11.However; you can use nineoldandroids library project which is a proxy for this animations (and quite good).
Or you can simply use Animation class
UPDATE:
i missed the part you said u already use nineoldandroids. did you check your imports maybe you imported the native
ViewPropertyAnimation
it may cause problem too. That libray uses the native api if api level is >11 you dont need to import native one.
You need to import ViewHelper class of nineoldandroid like
import com.nineoldandroids.view.ViewHelper;
and then use following code
ViewHelper.setTranslationX( Your View, 0.0F);
ViewHelper.setTranslationY( Your View, height);
ViewHelper.setRotationX(Your View,45.0F);
ViewHelper.setScaleX(Your View,0.7F);
ViewHelper.setScaleY(Your View,0.55F);
instead of
v.setTranslationX(0.0F);
v.setTranslationY(height);
v.setRotationX(45.0F);
v.setScaleX(0.7F);
v.setScaleY(0.55F);
Since Nineoldandroids allow api 1> use animation methods.
However, I run in API8 and Force Close is occured!
HERE is the solution, it's because of Nineoldandroids
[http://answer.techwikihow.com/962376/nineoldandroids-animation-working-api10.html][1]
Use Library of NineOldndroids Folder as Dependency instead of .jar,
Modification some code in
ObjectAnimator.Class
by following the answer in the link above!