Android Development _animation

Source: Internet
Author: User

Http://www.cnblogs.com/hongten/gallery/image/112163.html

Project structure:

Http://www.cnblogs.com/hongten/gallery/image/112162.html

P1.png is added yourself, of course, you can also use other pictures

Main.xml

1 <?xml version= "1.0" encoding= "Utf-8"?>
2 <relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
3 android:id= "@+id/layoutid"
4 android:orientation= "Vertical"
5 android:layout_width= "Fill_parent"
6 android:layout_height= "Fill_parent"
7 >
8 <linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
9 android:layout_width= "Fill_parent"
Ten android:layout_height= "Fill_parent"
>
<!--add Pictures--
<button
Android:id= "@+id/btn_add"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "Add Picture"
/>
<!--Delete pictures--
<button
Android:id= "@+id/btn_delete"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "Delete Picture"
/>
</LinearLayout>
<!--show Pictures-
<imageview
Android:id= "@+id/iv_image"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:layout_centerinparent= "true"
android:layout_margintop= "120dip"
android:layout_marginleft= "50dip"
android:src= "@drawable/p1"
Approx./>
Panax Notoginseng </RelativeLayout>

Main.java

1 package com.b510;
2
3 Import android.app.Activity;
4 Import Android.os.Bundle;
5 Import Android.util.Log;
6 Import Android.view.View;
7 Import Android.view.ViewGroup;
8 Import Android.view.View.OnClickListener;
9 Import android.view.animation.AlphaAnimation;
Ten import android.view.animation.Animation;
Import Android.view.animation.Animation.AnimationListener;
Import Android.widget.Button;
Import Android.widget.ImageView;
14
public class Main extends Activity {
private static final String tag= "Main";
17/** Add Picture */
Private Button AddButton;
19/** Delete Picture */
Private Button DeleteButton;
21/** Display Picture */
The private ImageView ImageView;
/** raletvielayout Layout, he is included in the </br>button,imageview control, defined in the Main.xml file */
Private ViewGroup ViewGroup;
25
/** called when the activity is first created. */
@Override
-public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
31//Find the corresponding control from the Main.xml file
AddButton = (Button) Findviewbyid (R.id.btn_add);
DeleteButton = (Button) Findviewbyid (r.id.btn_delete);
ImageView = (ImageView) Findviewbyid (r.id.iv_image);
ViewGroup = (viewgroup) Findviewbyid (R.id.layoutid);
36
PNS Deletebutton.setonclicklistener (New Onclicklistener () {
38
@Override
+ public void OnClick (View v) {
41//Declare a Alphaanimation object, from completely opaque to fully transparent
Alphaanimation alphaanimation = new Alphaanimation (1.0f, 0.0f);
43//Set animation duration to 2 seconds
Alphaanimation.setduration (2000);
45//Before performing the animation, delay 0.5 seconds clock
Alphaanimation.setstartoffset (500);
47//Set listener for animation objects
Alphaanimation.setanimationlistener (New Animationlistener () {
@Override
public void Onanimationstart (Animation Animation) {
Wuyi log.i (TAG, "start");
52}
53
@Override
public void Onanimationrepeat (Animation Animation) {
LOG.I (TAG, "repeat");
57}
58
@Override
public void Onanimationend (Animation Animation) {
LOG.I (TAG, "end");
62//Remove ImageView from ViewGroup
Viewgroup.removeview (ImageView);
64}
65});
Imageview.startanimation (alphaanimation);
67}
68});
69
Addbutton.setonclicklistener (New Onclicklistener () {
@Override
The public void OnClick (View v) {
73//Declare a Alphaanimation object, from fully transparent to completely opaque
Alphaanimation alphaanimation = new Alphaanimation (0.0f, 1.0f);
75//Set animation duration to 2 seconds
Alphaanimation.setduration (2000);
77//Before performing the animation, delay 0.5 seconds clock
Alphaanimation.setstartoffset (500);
Viewgroup.addview (ImageView);
80//Start animation
Bayi imageview.startanimation (alphaanimation);
82}
83});
84}
85}

Run effect

1. Initialization

Http://www.cnblogs.com/hongten/gallery/image/112164.html

2. Click the Delete Picture button

Http://www.cnblogs.com/hongten/gallery/image/112165.html

3. Click the Add Picture button

Http://www.cnblogs.com/hongten/gallery/image/112166.html

4. Background operating conditions

Http://www.cnblogs.com/hongten/gallery/image/112167.html

When we click the Delete button, the Android system automatically calls the Onanimationstart () method and then calls the Onanimationend () method.

9.5 Alphaanimation class: Transparency Change Animation class

The Alphaanimation class is an animated class of transparency changes in an Android system that controls the transparency of a view object that inherits from the animation class. Many of the methods in the Alphaanimation class are consistent with the animation class, and the most common method in this class is the Alphaanimation construction method.

"Basic Syntax" public alphaanimation (float fromalpha, float toalpha)

Parameter description

Fromalpha: The transparency of the start time, the value range 0~1.

Toalpha: The transparency of the end time, the value range 0~1.

The example below demonstrates how to set a simple gradient transparency animation effect by using code.

  1. public class Firstactivity extends Activity {
  2. /** called when the activity is first created. */
  3. @Override
  4. public void OnCreate (Bundle savedinstancestate) {//overloaded OnCreate method
  5. Super.oncreate (savedinstancestate);
  6. Setcontentview (R.layout.main);
  7. Final ImageView image= (ImageView) Findviewbyid (r.id.imageview1);//imageview Object
  8. Button btn1= (button) Findviewbyid (R.id.button1); Button Object
  9. Button btn2= (button) Findviewbyid (R.id.button2);
  10. Final Animation alphaanimation=new Alphaanimation (0.1f,1.0f); Set transparency animation effects
  11. Btn1.setonclicklistener (New View.onclicklistener () {//Set listener
  12. @Override
  13. public void OnClick (View v) {
  14. TODO auto-generated Method Stub
  15. Alphaanimation.setduration (30000); Set duration
  16. Image.setanimation (alphaanimation); Set animation
  17. Alphaanimation.startnow (); Start animation
  18. }
  19. });
  20. Btn2.setonclicklistener (New View.onclicklistener () {//Set listener
  21. @Override
  22. public void OnClick (View v) {
  23. TODO auto-generated Method Stub
  24. Scaleanimation.cancel (); Cancel Animation execution
  25. }
  26. });
  27. }
  28. }

In this code, you first create an animated object with a transparency change through the alphaanimation construction method. Then, the duration of the animation is set in the first button listener, and then the animation is started. Cancels the animation in a second button listener. The reader runs this code and will see that the transparency of the image is gradually changing, as shown in 9.11. Finally, the picture becomes completely opaque when stopped, as shown in 9.12.

Figure 9.11 Transparency Gradient animation
Figure 9.12 Image Raw Transparency

Android Development _animation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.