21_Android中常见对话框,光传感器,通过重力感应器编写出指南针应用,帧动画,通过Jav代码的方式编写补间动画,通过XML的方式编写补间动画

简介: 1 关于常见的对话框,主要有:常见的对话框,单选对话框,多选对话框,进度条对话框(转圈类型的),带进度条的对话框。案例结构:完成如下结构的案例,将所有的案例都测试一下:2 编写MainActivity,代码如下: package com.itheima.dialog;   import android.app.Activity; import and


1 关于常见的对话框,主要有:

常见的对话框,单选对话框,多选对话框,进度条对话框(转圈类型的),带进度条的对话框。

案例结构:

完成如下结构的案例,将所有的案例都测试一下:

2 编写MainActivity,代码如下:

package com.itheima.dialog;

 

import android.app.Activity;

import android.app.AlertDialog;

import android.app.ProgressDialog;

import android.app.AlertDialog.Builder;

import android.content.DialogInterface;

import android.content.DialogInterface.OnClickListener;

import android.content.DialogInterface.OnMultiChoiceClickListener;

import android.os.Bundle;

import android.view.View;

import android.widget.Toast;

 

public class MainActivity extends Activity {

 

    protected void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.activity_main);

    }

 

    public void click1(View view) {

       // 对话框的创建器

       AlertDialog.Builder builder = new Builder(this);

       builder.setTitle("我是对话框");

       builder.setMessage("对话框显示的内容");

       // 设置点击确定按钮后制定的动作

       builder.setPositiveButton("确定", new OnClickListener() {

 

           @Override

           public void onClick(DialogInterface dialog, int which) {

              Toast.makeText(getApplicationContext(), "确定被点击了", 0).show();

           }

       });

        builder.setNegativeButton("取消", new OnClickListener() {// 设置取消按钮

 

                  @Override

                  public void onClick(DialogInterface dialog, int which) {

                     // 什么都不写默认实现的就是关闭掉对话框

                     Toast.makeText(getApplicationContext(), "点击了取消按钮",

                            Toast.LENGTH_LONG).show();

                  }

              });

       builder.setCancelable(false);

       builder.create().show();

    }

 

    /**

     * 单选对话框

     *

     * @param view

     */

    public void click2(View view) {

       // 对话框的创建器

       AlertDialog.Builder builder = new Builder(this);

       builder.setTitle("请选择您的性别");

       final String[] items = { "", "", "未知" };

        //这里的1表示默认选中的是哪个,0:表示选中的是第一个

       builder.setSingleChoiceItems(items, 1, new OnClickListener() {

           @Override

           public void onClick(DialogInterface dialog, int which) {

              Toast.makeText(getApplicationContext(), "您的性别:" + items[which],

                     0).show();

              dialog.dismiss();

           }

       });

       builder.create().show();

    }

 

    /**

     * 多选对话框

     * @param view

     */

    public void click3(View view) {

       // 对话框的创建器

       AlertDialog.Builder builder = new Builder(this);

       builder.setTitle("请选择你最爱吃的水果");

       final String[] items = { "苹果", "", "菠萝", "香蕉", "黄瓜" };

       final boolean[] result = new boolean[] { true, false, true, false,false};

       builder.setMultiChoiceItems(items, result,

              new OnMultiChoiceClickListener() {

                  public void onClick(DialogInterface dialog, int which,

                         boolean isChecked) {

                     Toast.makeText(getApplicationContext(),

                            items[which] + isChecked, 0).show();

                     result[which] = isChecked;

                  }

              });

       builder.setPositiveButton("提交", new OnClickListener() {

           public void onClick(DialogInterface dialog, int which) {

              StringBuffer sb = new StringBuffer();

               for (int i = 0; i < result.length; i++) {

                  if (result[i]) {

                     sb.append(items[i] + ",");

                  }

              }

              Toast.makeText(getApplicationContext(),

                     "您选中了," + sb.toString(), 0).show();

           }

       });

       // builder.create().show();

 

       builder.show();

    }

 

    // 进度条对话框

    public void click4(View view) {

       ProgressDialog pd = new ProgressDialog(this);

       pd.setTitle("提醒");

       pd.setMessage("正在加载数据...请稍等。");

       pd.show();

    }

 

    // 带进度的进度条对话框

    public void click5(View view) {

       final ProgressDialog pd = new ProgressDialog(this);

       pd.setTitle("提醒");

       pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

       pd.setMax(100);

       pd.setMessage("正在加载数据...请稍等。");

       pd.show();

       new Thread() {

           public void run() {

              for (int i = 0; i < 100; i++) {

                  try {

                      Thread.sleep(40);

                  } catch (InterruptedException e) {

                     e.printStackTrace();

                  }

                  pd.setProgress(i);

              }

              pd.dismiss();

           };

       }.start();

    }

}

==============================================================================

1 光传感器

编写布局文件activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".MainActivity" >

 

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_centerHorizontal="true"

        android:layout_centerVertical="true"

        android:text="@string/hello_world" />

 

</RelativeLayout>

2 编写MainActivity,代码如下:

package com.itheima.sensor;

 

import android.app.Activity;

import android.hardware.Sensor;

import android.hardware.SensorEvent;

import android.hardware.SensorEventListener;

import android.hardware.SensorManager;

import android.os.Bundle;

 

public class MainActivity extends Activity {

         private SensorManager sm;

         private MyListener listener;

 

         @Override

         protected void onCreate(Bundle savedInstanceState) {

                   super.onCreate(savedInstanceState);

                   setContentView(R.layout.activity_main);

                   sm = (SensorManager) getSystemService(SENSOR_SERVICE);

                   //光线传感器

                   Sensor sensor = sm.getDefaultSensor(Sensor.TYPE_LIGHT);

                   listener = new MyListener();

                   sm.registerListener(listener, sensor,SensorManager.SENSOR_DELAY_UI);

         }

        

         private class MyListener implements SensorEventListener {

                   public void onSensorChanged(SensorEvent event) {

                            float light = event.values[0];

                            System.out.println("light:" + light);

                   }

                  

                   public void onAccuracyChanged(Sensor sensor, int accuracy) {

                           

                   }       

         }

        

         @Override

         protected void onDestroy() {

                   sm.unregisterListener(listener);

                   listener = null;

                   super.onDestroy();

         }

}

==============================================================================

1  Android指南针,案例效果:

2 编写布局文件,代码如下(activity_main.xml):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:background="#000000"

    tools:context=".MainActivity" >

 

    <ImageView

        android:id="@+id/iv"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_centerHorizontal="true"

        android:layout_centerVertical="true"

        android:src="@drawable/zn" />

   

</RelativeLayout>

3 编写MainActivity,代码如下:

package com.itheima.sensor;

 

import android.app.Activity;

import android.hardware.Sensor;

import android.hardware.SensorEvent;

import android.hardware.SensorEventListener;

import android.hardware.SensorManager;

import android.os.Bundle;

import android.view.animation.Animation;

import android.view.animation.RotateAnimation;

import android.widget.ImageView;

 

public class MainActivity extends Activity {

         private SensorManager sm;

         private MyListener listener;

         private ImageView iv;

        

         @SuppressWarnings("deprecation")

         @Override

         protected void onCreate(Bundle savedInstanceState) {

                   super.onCreate(savedInstanceState);

                   setContentView(R.layout.activity_main);

                   sm = (SensorManager) getSystemService(SENSOR_SERVICE);

                   iv = (ImageView) findViewById(R.id.iv);

                   //方向传感器

                   Sensor sensor = sm.getDefaultSensor(Sensor.TYPE_ORIENTATION);

                   listener = new MyListener();

                   sm.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_GAME);

         }

 

         private class MyListener implements SensorEventListener {

                   float lastangle = 0;

                   @Override

                   public void onSensorChanged(SensorEvent event) {

                            // 0=North, 90=East, 180=South, 270=West 

                            float angle = event.values[0];//手机与正北方向的夹角

                            System.out.println("angle:"+angle);

                            RotateAnimation ra = new RotateAnimation(-lastangle, angle,

                                               Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

                            iv.startAnimation(ra);

                            lastangle = angle;

                   }

                  

                   @Override

                   public void onAccuracyChanged(Sensor sensor, int accuracy) {

                           

                   }

         }

        

         @Override

         protected void onDestroy() {

                   sm.unregisterListener(listener);

                   listener = null;

                   super.onDestroy();

         }

}

 

补间动画主要包括以下几种:

A (旋转)  B(透明度)  C(位移)  D(缩放)

编写一下案例:

1 Android布局文件activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".MainActivity" >

 

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:orientation="horizontal" >

 

        <Button

            android:onClick="rotate"

            android:layout_width="0dip"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="旋转" />

 

        <Button

            android:onClick="scale"

            android:layout_width="0dip"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="缩放" />

 

        <Button

            android:onClick="trans"

            android:layout_width="0dip"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="位移" />

 

        <Button

            android:onClick="alpha"

            android:layout_width="0dip"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="透明度" />

       

           <Button

            android:onClick="set"

            android:layout_width="0dip"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="组合动画" />

    </LinearLayout>

 

    <ImageView

        android:id="@+id/iv"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_centerHorizontal="true"

        android:layout_centerVertical="true"

        android:src="@drawable/ic_launcher" />

 

</RelativeLayout>

2 MainActivity代码如下:

package com.itheima.tween;

 

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.animation.AlphaAnimation;

import android.view.animation.Animation;

import android.view.animation.AnimationSet;

import android.view.animation.AnimationUtils;

import android.view.animation.RotateAnimation;

import android.view.animation.ScaleAnimation;

import android.view.animation.TranslateAnimation;

import android.widget.ImageView;

 

public class MainActivity extends Activity {

 

    private ImageView iv;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.activity_main);

       iv = (ImageView) findViewById(R.id.iv);

    }

 

    // 透明度动画

    public void alpha(View view) {

       // 最开始的透明度到最后的透明度,从0.0f1.0f,从透明到不透明

       Animation aa = new AlphaAnimation(0.0f, 1.0f);

       // 设置动画播放的时间

       aa.setDuration(2000);

       // 设置动画重复播放的次数,下面表示重复播放3次,表示重复播放2,如果是

       // -1(Animation.INFINITE)表示一直重复播放

       aa.setRepeatCount(3);

       // aa.setRepeatCount(Animation.INFINITE);

       // 如果不指定这个值,默认是重复播放的。下面表示:透明-->不透明-->透明

       aa.setRepeatMode(Animation.REVERSE);

       // true:界面为动画完成之后的效果

       aa.setFillAfter(true);

       // 开始播放

       iv.startAnimation(aa);

    }

 

    /**

     * 位移动画

     *

     * @param view

     */

    public void trans(View view) {

       // 下面表示x轴从0.0f-->1.0f;0.0f-->1.0f

       // android.view.animation.TranslateAnimation.TranslateAnimation(int

       // fromXType, float fromXValue, int toXType, float toXValue, int

       // fromYType, float fromYValue, int toYType, float toYValue)

       TranslateAnimation ta = new TranslateAnimation(

              Animation.RELATIVE_TO_PARENT, // 相对于父窗体

              0.0f, // 如果是320宽度的模拟器。这里0.0f表示是是父窗体的0%

              Animation.RELATIVE_TO_PARENT, // 还是相对于父窗体

              1.0f, // 表示父亲的100%

              Animation.RELATIVE_TO_PARENT, 0.0f,

              Animation.RELATIVE_TO_PARENT, 1.0f);

       ta.setDuration(2000); // 设置时间间隔

       ta.setRepeatCount(-1); // -1表示重复的操作

       // 倒叙播放

       ta.setRepeatMode(Animation.REVERSE);

       iv.startAnimation(ta);

    }

 

    // 缩放动画

    public void scale(View view) {

       ScaleAnimation sa = new ScaleAnimation(0.1f, // 缩放的时候最开始的比例

              2.0f, // 上面这两个参数x周表示从0.1倍到2

              0.1f, 2.0f, // y轴从0.1-->2.0

              Animation.RELATIVE_TO_SELF, // 后面4个参数的组合表示从自己中心点开始缩小放大

               0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

 

       sa.setDuration(2000); // 设置时间间隔

       sa.setRepeatCount(1); // -1表示重复的操作

       // 倒叙播放

       sa.setRepeatMode(Animation.REVERSE);

       iv.startAnimation(sa);

    }

 

    // 旋转动画

    public void rotate(View view) {

       RotateAnimation ra = new RotateAnimation(

              0, // 开始的角度

              360, // 旋转的解读

              Animation.RELATIVE_TO_SELF,

              0.0f,

              Animation.RELATIVE_TO_SELF,

              0.0f);

       ra.setDuration(2000);

       ra.setRepeatCount(1);

       ra.setRepeatMode(Animation.REVERSE);

       iv.startAnimation(ra);

    }

   

    //动画组合(包含多种动画)

    public void set(View view) {

       AnimationSet set = new AnimationSet(false);

       TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, -0.5f,

              Animation.RELATIVE_TO_PARENT, 0.5f,

              Animation.RELATIVE_TO_PARENT, -0.5f,

              Animation.RELATIVE_TO_PARENT, 0.5f);

       ta.setDuration(2000);

       ta.setRepeatCount(1);

       ta.setRepeatMode(Animation.REVERSE);

       ScaleAnimation sa = new ScaleAnimation(0.1f, 2.0f, 0.1f, 2.0f, Animation.RELATIVE_TO_SELF,

              0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

       sa.setDuration(2000);

       sa.setRepeatCount(1);

       sa.setRepeatMode(Animation.REVERSE);

       RotateAnimation ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF,

              0.0f, Animation.RELATIVE_TO_SELF, 0.0f);

       ra.setDuration(2000);

       ra.setRepeatCount(1);

       ra.setRepeatMode(Animation.REVERSE);

       set.addAnimation(ra);

       //set.addAnimation(ta);

       set.addAnimation(sa);

       iv.startAnimation(set);

    }

}

=============================================================================

1 除了通过代码的方式制作补间动画之外,还可以通过xml的方式制作补间动画。

案例:

2 下面通过如下结构的代码编写出上面的案例:

3 编写的布局文件activity_main.xml如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".MainActivity" >

 

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:orientation="horizontal" >

 

        <Button

            android:onClick="rotate"

            android:layout_width="0dip"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="旋转" />

 

        <Button

            android:onClick="scale"

            android:layout_width="0dip"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="缩放" />

 

        <Button

            android:onClick="trans"

            android:layout_width="0dip"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="位移" />

 

        <Button

            android:onClick="alpha"

            android:layout_width="0dip"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="透明度" />

       

           <Button

            android:onClick="set"

            android:layout_width="0dip"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="组合动画" />

    </LinearLayout>

 

    <ImageView

        android:id="@+id/iv"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_centerHorizontal="true"

        android:layout_centerVertical="true"

        android:src="@drawable/ic_launcher" />

 

</RelativeLayout>

4 编写透明度的xml文件alpha.xml

<?xml version="1.0" encoding="utf-8"?>

 

<!--

android:fromAlpha="0.0"  开始的透明度

android:toAlpha="1.0"    结束的透明度

android:duration="2000"  动画播放的时间

android:repeatCount="1"  动画重复的次数

android:repeatMode="reverse"    重复的模式

android:fillAfter="true"  

-->

<alpha xmlns:android="http://schemas.android.com/apk/res/android"

    android:fromAlpha="0.0"

    android:toAlpha="1.0"  

    android:duration="2000"

    android:repeatCount="2"

    android:repeatMode="reverse"

    android:fillAfter="true">

   

</alpha>

5 编写旋转的xml文件rotate.xml

<?xml version="1.0" encoding="utf-8"?>

<rotate xmlns:android="http://schemas.android.com/apk/res/android"

    android:fromDegrees="0"

    android:toDegrees="360"

    android:pivotX="50%"

    android:pivotY="50%"

    android:duration="2000"

    android:repeatCount="1"

    android:repeatMode="reverse" >

 

</rotate>

6 编写放大缩小的xml文件scale.xml

<?xml version="1.0" encoding="utf-8"?>

 

<!--

android:fromXScale="0.1"

android:toXScale="2.0"

android:fromYScale="0.1"

android:toYScale="2.0"

android:duration="2000"

android:pivotX="50%"

android:pivotY="50%"

android:repeatCount="1"

android:repeatMode="reverse"

-->

<scale xmlns:android="http://schemas.android.com/apk/res/android"

    android:fromXScale="0.1"

    android:toXScale="2.0"

    android:fromYScale="0.1"

    android:toYScale="2.0"

    android:duration="2000"

    android:pivotX="50%"

    android:pivotY="50%"

    android:repeatCount="1"

    android:repeatMode="reverse">

  

</scale>

7 编写位移的xml文件trans.xml

<?xml version="1.0" encoding="utf-8"?>

 

<!--

android:fromXDelta="-50%p"   左侧

android:toXDelta="50%p"      右侧

android:fromYDelta="0"       表示y轴方向上不变化

android:toYDelta="0"        

android:duration="2000"      播放2

android:repeatCount="1"      重复1

android:repeatMode="reverse"

 -->

<translate xmlns:android="http://schemas.android.com/apk/res/android"

    android:fromXDelta="-50%p"

    android:toXDelta="50%p"

    android:fromYDelta="0"

    android:toYDelta="0"

    android:duration="2000"

    android:repeatCount="1"

    android:repeatMode="reverse">

   

</translate>

8 编写组合动画set.xml

<?xml version="1.0" encoding="utf-8"?>

<set>

    <alpha

        xmlns:android="http://schemas.android.com/apk/res/android"

        android:duration="2000"

        android:fillAfter="true"

        android:fromAlpha="0.0"

        android:repeatCount="1"

        android:repeatMode="reverse"

        android:toAlpha="1.0" >

    </alpha>

 

    <rotate

        xmlns:android="http://schemas.android.com/apk/res/android"

        android:duration="2000"

        android:fromDegrees="0"

        android:pivotX="50%"

        android:pivotY="50%"

        android:repeatCount="1"

        android:repeatMode="reverse"

        android:toDegrees="360" >

    </rotate>

 

    <scale

        xmlns:android="http://schemas.android.com/apk/res/android"

        android:duration="2000"

        android:fromXScale="0.1"

        android:fromYScale="0.1"

        android:pivotX="50%"

        android:pivotY="50%"

        android:repeatCount="1"

        android:repeatMode="reverse"

        android:toXScale="2.0"

        android:toYScale="2.0" >

    </scale>

 

    <translate

        xmlns:android="http://schemas.android.com/apk/res/android"

        android:duration="2000"

        android:fromXDelta="-50%p"

        android:fromYDelta="0"

        android:repeatCount="1"

        android:repeatMode="reverse"

        android:toXDelta="50%p"

        android:toYDelta="0" >

    </translate>

 

</set>

9 编写MainActivity,代码如下:

package com.itheima.tween;

 

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.animation.Animation;

import android.view.animation.AnimationUtils;

import android.widget.ImageView;

 

public class MainActivity extends Activity {

 

         private ImageView iv;

 

         @Override

         protected void onCreate(Bundle savedInstanceState) {

                   super.onCreate(savedInstanceState);

                   setContentView(R.layout.activity_main);

                   iv = (ImageView) findViewById(R.id.iv);

         }

 

         // 透明度动画

         public void alpha(View view) {

                   Animation aa = AnimationUtils.loadAnimation(this, R.anim.alpha);

                   iv.startAnimation(aa);

         }

 

         /**

          * 位移动画

          *

          * @param view

          */

         public void trans(View view) {

                   Animation ta = AnimationUtils.loadAnimation(this, R.anim.trans);

                   iv.startAnimation(ta);

         }

 

         // 缩放动画

         public void scale(View view) {

                   Animation sa = AnimationUtils.loadAnimation(this, R.anim.scale);

                   iv.startAnimation(sa);

         }

 

         // 旋转动画

         public void rotate(View view) {

                   Animation ra = AnimationUtils.loadAnimation(this, R.anim.rotate);

                   iv.startAnimation(ra);

         }

        

         //动画组合(包含多种动画)

         public void set(View view) {

                   Animation set = AnimationUtils.loadAnimation(this, R.anim.set);

                   iv.startAnimation(set);

         }

}

清单文件略

帧动画(主要是在xml中编写:animation-list),编写如下案例:

1 编写布局文件activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".MainActivity" >

 

    <ImageView

        android:id="@+id/iv"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_centerHorizontal="true"

        android:layout_centerVertical="true"

       />

 

</RelativeLayout>

2 drawable中编写帧动画的xml文件

项目中的结构如下:

<?xml version="1.0" encoding="utf-8"?>

<!--

android:oneshot="false"  表示重复性的播放  如果为true表示只播放一次

 -->

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"

    android:oneshot="false"

    >

  

    <!-- 下面表示使用指定的图片播放200毫秒 -->

    <item

        android:drawable="@drawable/girl_1"

        android:duration="200"/>

    <item

        android:drawable="@drawable/girl_2"

        android:duration="200"/>

    <item

        android:drawable="@drawable/girl_3"

        android:duration="200"/>

    <item

        android:drawable="@drawable/girl_4"

        android:duration="200"/>

    <item

        android:drawable="@drawable/girl_5"

        android:duration="200"/>

    <item

        android:drawable="@drawable/girl_6"

        android:duration="400"/>

    <item

        android:drawable="@drawable/girl_7"

        android:duration="400"/>

    <item

        android:drawable="@drawable/girl_6"

        android:duration="400"/>

    <item

        android:drawable="@drawable/girl_7"

        android:duration="400"/>

    <item

        android:drawable="@drawable/girl_6"

        android:duration="400"/>

    <item

        android:drawable="@drawable/girl_7"

        android:duration="400"/>

    <item

        android:drawable="@drawable/girl_8"

        android:duration="200"/>

    <item

        android:drawable="@drawable/girl_9"

        android:duration="200"/>

    <item

        android:drawable="@drawable/girl_10"

        android:duration="200"/>

    <item

        android:drawable="@drawable/girl_11"

        android:duration="200"/>

   

</animation-list>

2 编写MainActivity,代码如下:

package com.itheima.frameanimation;

 

import android.app.Activity;

import android.graphics.drawable.AnimationDrawable;

import android.os.Bundle;

import android.view.MotionEvent;

import android.widget.ImageView;

 

public class MainActivity extends Activity {

    private ImageView iv;

    private AnimationDrawable mAnimationDrawable;

   

    protected void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.activity_main);

 

       iv = (ImageView) findViewById(R.id.iv);

       // xml文件的动画资源设置为iv背景

       iv.setBackgroundResource(R.drawable.girl);

       // 获取设置的动画资源。 执行可能需要花费一定的时间

       mAnimationDrawable = (AnimationDrawable) iv.getBackground();

    }

   

    public boolean onTouchEvent(MotionEvent event) {

       if (event.getAction() == MotionEvent.ACTION_DOWN) {

           mAnimationDrawable.start();

           return true;

       }

       return super.onTouchEvent(event);

    }

}

 

 


目录
相关文章
|
2天前
|
移动开发 Java Android开发
构建高效Android应用:采用Kotlin协程优化网络请求
【4月更文挑战第24天】 在移动开发领域,尤其是对于Android平台而言,网络请求是一个不可或缺的功能。然而,随着用户对应用响应速度和稳定性要求的不断提高,传统的异步处理方式如回调地狱和RxJava已逐渐显示出局限性。本文将探讨如何利用Kotlin协程来简化异步代码,提升网络请求的效率和可读性。我们将深入分析协程的原理,并通过一个实际案例展示如何在Android应用中集成和优化网络请求。
|
2天前
|
调度 Android开发 开发者
构建高效Android应用:探究Kotlin协程的优势与实践
【4月更文挑战第24天】随着移动开发技术的不断演进,提升应用性能和用户体验已成为开发者的核心任务。在Android平台上,Kotlin语言凭借其简洁性和功能性成为主流选择之一。特别是Kotlin的协程功能,它为异步编程提供了一种轻量级的解决方案,使得处理并发任务更加高效和简洁。本文将深入探讨Kotlin协程在Android开发中的应用,通过实际案例分析协程如何优化应用性能,以及如何在项目中实现协程。
|
3天前
|
存储 缓存 安全
Android系统 应用存储路径与权限
Android系统 应用存储路径与权限
6 0
Android系统 应用存储路径与权限
|
3天前
|
存储 安全 Android开发
Android系统 自定义系统和应用权限
Android系统 自定义系统和应用权限
18 0
|
8天前
|
缓存 移动开发 Android开发
构建高效Android应用:从优化用户体验到提升性能表现
【4月更文挑战第18天】 在移动开发的世界中,打造一个既快速又流畅的Android应用并非易事。本文深入探讨了如何通过一系列创新的技术策略来提升应用性能和用户体验。我们将从用户界面(UI)设计的简约性原则出发,探索响应式布局和Material Design的实践,再深入剖析后台任务处理、内存管理和电池寿命优化的技巧。此外,文中还将讨论最新的Android Jetpack组件如何帮助开发者更高效地构建高质量的应用。此内容不仅适合经验丰富的开发者深化理解,也适合初学者构建起对Android高效开发的基础认识。
8 0
|
8天前
|
移动开发 Android开发 开发者
构建高效Android应用:采用Kotlin进行内存优化的策略
【4月更文挑战第18天】 在移动开发领域,性能优化一直是开发者关注的焦点。特别是对于Android应用而言,由于设备和版本的多样性,确保应用流畅运行且占用资源少是一大挑战。本文将探讨使用Kotlin语言开发Android应用时,如何通过内存优化来提升应用性能。我们将从减少不必要的对象创建、合理使用数据结构、避免内存泄漏等方面入手,提供实用的代码示例和最佳实践,帮助开发者构建更加高效的Android应用。
14 0
|
10天前
|
缓存 移动开发 Java
构建高效的Android应用:内存优化策略
【4月更文挑战第16天】 在移动开发领域,尤其是针对资源有限的Android设备,内存优化是提升应用性能和用户体验的关键因素。本文将深入探讨Android应用的内存管理机制,分析常见的内存泄漏问题,并提出一系列实用的内存优化技巧。通过这些策略的实施,开发者可以显著减少应用的内存占用,避免不必要的后台服务,以及提高垃圾回收效率,从而延长设备的电池寿命并确保应用的流畅运行。
|
12天前
|
搜索推荐 开发工具 Android开发
安卓即时应用(Instant Apps)开发指南
【4月更文挑战第14天】Android Instant Apps让用户体验部分应用功能而无需完整下载。开发者需将应用拆分成模块,基于已上线的基础应用构建。使用Android Studio的Instant Apps Feature Library定义模块特性,优化代码与资源以减小模块大小,同步管理即时应用和基础应用的版本。经过测试,可发布至Google Play Console,提升用户便利性,创造新获客机会。
|
13天前
|
Java API 调度
安卓多线程和并发处理:提高应用效率
【4月更文挑战第13天】本文探讨了安卓应用中多线程和并发处理的优化方法,包括使用Thread、AsyncTask、Loader、IntentService、JobScheduler、WorkManager以及线程池。此外,还介绍了RxJava和Kotlin协程作为异步编程工具。理解并恰当运用这些技术能提升应用效率,避免UI卡顿,确保良好用户体验。随着安卓技术发展,更高级的异步处理工具将助力开发者构建高性能应用。
|
13天前
|
编解码 人工智能 测试技术
安卓适配性策略:确保应用在不同设备上的兼容性
【4月更文挑战第13天】本文探讨了提升安卓应用兼容性的策略,包括理解平台碎片化、设计响应式UI(使用dp单位,考虑横竖屏)、利用Android SDK的兼容工具(支持库、资源限定符)、编写兼容性代码(运行时权限、设备特性检查)以及优化性能以适应低端设备。适配性是安卓开发的关键,通过这些方法可确保应用在多样化设备上提供一致体验。未来,自动化测试和AI将助力应对设备碎片化挑战。