解决Android帧动画在Oncreate中启动只显示第一帧 和 Android中获取设备的IP

来源:互联网 发布:我的世界手机版女仆js 编辑:程序博客网 时间:2024/04/30 13:40

http://berdy.iteye.com/blog/1894802

做了个简单的帧动画,在onCreate方法中start,发现只能看到第一帧 

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <animation-list xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:oneshot="false" >  
  4.   
  5.     <item  
  6.         android:drawable="@drawable/fire01"  
  7.         android:duration="200"/>  
  8.     <item  
  9.         android:drawable="@drawable/fire02"  
  10.         android:duration="200"/>  
  11.     <item  
  12.         android:drawable="@drawable/fire03"  
  13.         android:duration="200"/>  
  14.     <item  
  15.         android:drawable="@drawable/fire04"  
  16.         android:duration="200"/>  
  17.     <item  
  18.         android:drawable="@drawable/fire05"  
  19.         android:duration="200"/>  
  20.     <item  
  21.         android:drawable="@drawable/fire06"  
  22.         android:duration="200"/>  
  23.     <item  
  24.         android:drawable="@drawable/fire07"  
  25.         android:duration="200"/>  
  26.     <item  
  27.         android:drawable="@drawable/fire08"  
  28.         android:duration="200"/>  
  29.     <item  
  30.         android:drawable="@drawable/fire09"  
  31.         android:duration="200"/>  
  32.   
  33. </animation-list>  


Xml代码  收藏代码
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     >  
  6.     <ImageView  
  7.         android:id="@+id/fire_img"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_centerInParent="true"  
  11.         android:background="@drawable/fire_anim" />  
  12. </RelativeLayout>  


Java代码  收藏代码
  1. ImageView fireImg = null;  
  2.   
  3. @Override  
  4. protected void onCreate(Bundle savedInstanceState) {  
  5.     super.onCreate(savedInstanceState);  
  6.     setContentView(R.layout.activity_main);  
  7.   
  8.     ImageView fireImg = (ImageView) findViewById(R.id.fire_img);  
  9.     final AnimationDrawable animDrawable = (AnimationDrawable) fireImg  
  10.             .getBackground();  
  11.     animDrawable.start();  
  12. }  


stackoverflow 上发现一个解决办法 
Java代码  收藏代码
  1. @Override  
  2. protected void onCreate(Bundle savedInstanceState) {  
  3.     super.onCreate(savedInstanceState);  
  4.     setContentView(R.layout.activity_main);  
  5.   
  6.     ImageView fireImg = (ImageView) findViewById(R.id.fire_img);  
  7.     final AnimationDrawable animDrawable = (AnimationDrawable) fireImg  
  8.             .getBackground();  
  9.     fireImg.post(new Runnable() {  
  10.         @Override  
  11.         public void run() {  
  12.             animDrawable.start();  
  13.         }  
  14.     });  
  15. }  


或者在Activity中的onWindowFocusChanged方法中start 
Java代码  收藏代码
  1. @Override  
  2. public void onWindowFocusChanged(boolean hasFocus) {  
  3.     super.onWindowFocusChanged(hasFocus);  
  4.     if (fireImg != null) {  
  5.         AnimationDrawable animDrawable = (AnimationDrawable) fireImg  
  6.                 .getBackground();  
  7.         animDrawable.start();  
  8.     }  
  9. }  



在wifi环境下,可以通过WifiInfo来获取设备的ip 

Java代码  收藏代码
  1. public String getIpAddress() {  
  2.     WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);  
  3.     WifiInfo wifiInfo = wifiManager.getConnectionInfo();  
  4.     int ipAddress = wifiInfo.getIpAddress();  
  5.     int[] ipAddr = new int[4];  
  6.     ipAddr[0] = ipAddress & 0xFF;  
  7.     ipAddr[1] = (ipAddress >> 8) & 0xFF;  
  8.     ipAddr[2] = (ipAddress >> 16) & 0xFF;  
  9.     ipAddr[3] = (ipAddress >> 24) & 0xFF;  
  10.     return new StringBuilder().append(ipAddr[0]).append(".").append(ipAddr[1]).append(".").append(ipAddr[2])  
  11.             .append(".").append(ipAddr[3]).append(".").toString();  
  12.   
  13. }  


执行上面的代码需要 
Xml代码  收藏代码
  1. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />  


当然也可通过jdk总的NetworkInterface来获取的,就是遍历所有的网络接口,获取到非loopback ip 
Java代码  收藏代码
  1. public String getLocalIpAddress() {  
  2.     try {  
  3.         for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {  
  4.             NetworkInterface intf = en.nextElement();  
  5.             for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {  
  6.                 InetAddress inetAddress = enumIpAddr.nextElement();  
  7.                 if (!inetAddress.isLoopbackAddress()) {  
  8.                     return inetAddress.getHostAddress().toString();  
  9.                 }  
  10.             }  
  11.         }  
  12.     } catch (SocketException ex) {  
  13.         Log.e("", ex.toString());  
  14.     }  
  15.     return null;  
  16. }  

原创粉丝点击