项目中,需要做一个应用开始的过场动画,俗称splash。
抽出来做个demo
效果:
主要代码
package com.example.zidingyiflashview; import java.io.IOException; import java.io.InputStream; import java.util.Vector; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import junit.framework.Assert; import android.content.Context; import android.content.pm.PackageManager; import android.content.res.AssetManager; import android.graphics.Canvas; import android.util.AttributeSet; import android.view.View; public class FlashView extends View { Vector<ImageItem> iis=new Vector<ImageItem>(); public FlashView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub init(context); } public FlashView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub init(context); } public FlashView(Context context) { super(context); // TODO Auto-generated constructor stub init(context); } private void init(Context context) { // TODO Auto-generated method stub //1.读json数据,解析json,将解析的值封装到imageItem中。在imageItem中设置值,在imageItem中draw AssetManager assets = context.getAssets(); String fileName="splash/splash.json"; try { InputStream is = assets.open(fileName); int available = is.available(); byte[] buffer=new byte[available]; is.read(buffer); is.close(); //解析json String JStr= new String(buffer,"UTF-8"); JSONObject jObject=new JSONObject(JStr); JSONArray jsonArray = jObject.getJSONArray("images"); for(int i=0;null!=jsonArray&&i<jsonArray.length();i++){ JSONObject jObjectArray = jsonArray.getJSONObject(i); ImageItem ii = ImageItem.parseImageItem(jObjectArray); iis.add(ii); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); for(int i=0;i<iis.size();i++){ iis.get(i).onDraw(getContext(),canvas,getWidth(), getHeight(), "assets://splash/"); } } }
package com.example.zidingyiflashview; import java.io.IOException; import java.io.InputStream; import org.json.JSONObject; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Matrix; public class ImageItem { /** * 文件名 */ private String name; /** * 长、宽 */ private float[] load; /** * 居中等 */ private int algin; /** * 缩放 */ private float scale; public static ImageItem parseImageItem(JSONObject jsonObject){ ImageItem ii=new ImageItem(); ii.name=jsonObject.optString("name",null); String optString = jsonObject.optString("load",null); //将load封装到float[]里面 ii.load=parseIntArray(optString,4); ii.scale = (float)jsonObject.optDouble("scale",0); String strAlign = jsonObject.optString("align", ""); if("center".equals(strAlign)){ ii.algin=Align.CENTER; }else if("centerX".equals(strAlign)){ ii.algin=Align.CENTERX; }else if("centerY".equals(strAlign)){ ii.algin=Align.CENTERY; }else{ ii.algin=Align.NONE; } if(null==ii.name||"".equals(ii.name)){ return null; } return ii; } private static float[] parseIntArray(String optString, int count) { // TODO Auto-generated method stub try { float[] ft=new float[count]; String[] split = optString.split(","); for(int i=0;i<count;i++){ ft[i]=Float.parseFloat(split[i].trim()); } return ft; } catch (Exception e) { // TODO Auto-generated catch block } return null; } public void onDraw(Context context,Canvas canvas,int width,int height,String baseDir){ Bitmap bitmap=getBitMap(context,baseDir); int w = bitmap.getWidth(); int h = bitmap.getHeight(); if(null!=load){ if(w<load[0]||w>load[1]||h<load[2]||h>load[3]){ return; } } Matrix matrix=new Matrix(); if(scale==0F){ //占满空间 float d1=width*1.0F/w; float d2=height*1.0F/h; scale=Math.max(d1, d2); } matrix.setScale(scale, scale); //移动 if(algin==Align.CENTER){ matrix.postTranslate((width-w*scale)/2, (height-h*scale)/2); }else if(algin==Align.CENTERX){ matrix.postTranslate((width-w*scale)/2, 0); }else if(algin==Align.CENTERY){ matrix.postTranslate(0, (height-h*scale)/2); } canvas.drawBitmap(bitmap, matrix, null); } /** * 获取图片 * @param baseDir * @param context * @return */ private Bitmap getBitMap(Context context, String baseDir) { // TODO Auto-generated method stub try { InputStream is = context.getAssets().open((baseDir+name).substring("assets://".length())); Bitmap bitMap = BitmapFactory.decodeStream(is); is.close(); return bitMap; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } }
http://download.csdn.net/detail/pangzaifei/6930265