_15实现四种动画效果 AppExplorer0.4源码下载
_16通过资源文件实现动画效果
_17LayoutAnimation的使用
1 package cn.explorer; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 import android.app.Activity; 6 import android.app.AlertDialog; 7 import android.net.Uri; 8 import android.os.Bundle; 9 import android.os.Handler; 10 import android.os.Message; 11 12 import android.app.ProgressDialog; 13 14 import android.content.pm.ActivityInfo; 15 import android.content.pm.ApplicationInfo; 16 import android.content.pm.PackageInfo; 17 import android.content.pm.PackageManager; 18 import android.content.ComponentName; 19 import android.content.Context; 20 import android.content.DialogInterface; 21 import android.content.Intent; 22 23 import android.view.View; 24 import android.view.View.OnClickListener; 25 import android.view.ViewGroup; 26 import android.view.Window; 27 import android.view.WindowManager; 28 import android.view.LayoutInflater; 29 import android.view.animation.*; 30 31 import android.widget.AdapterView; 32 import android.widget.AdapterView.OnItemClickListener; 33 import android.widget.GridView; 34 import android.widget.BaseAdapter; 35 import android.widget.ListView; 36 import android.widget.TextView; 37 import android.widget.ImageView; 38 import android.widget.ImageButton; 39 import android.widget.Toast; 40 41 42 public class ShowAppActivity extends Activity implements Runnable,OnItemClickListener{ 43 private static final int SEARCH_APP = 0; 44 45 private static final int DELETE_APP = 1; 46 47 GridView gv; 48 49 ListView lv; 50 51 private List<PackageInfo> packageInfos; 52 53 private List<PackageInfo> userPackageInfos; 54 55 private List<PackageInfo> showPackageInfos; /*专门显示 程序package name*/ 56 57 private ProgressDialog pd; 58 59 ImageButton ib_change_category; 60 ImageButton ib_change_view; 61 62 private boolean allApplication = true; 63 private boolean isListView = false; 64 65 private Handler mHandler = new Handler() { 66 67 public void handleMessage(Message msg) { 68 super.handleMessage(msg); 69 if (msg.what == SEARCH_APP) { 70 showPackageInfos = packageInfos; 71 gv.setAdapter(new GridViewAdapter(ShowAppActivity.this,showPackageInfos)); 72 lv.setAdapter(new ListViewAdapter(ShowAppActivity.this,showPackageInfos)); 73 pd.dismiss(); 74 setProgressBarIndeterminateVisibility(false); /*在不需要的显示进度条的时候调用这个方法*/ 75 } 76 if (msg.what == DELETE_APP) { 77 System.out.println("Delete APP Success!!"); 78 } 79 } 80 }; 81 82 public void onCreate(Bundle savedInstanceState) { 83 super.onCreate(savedInstanceState); 84 85 // requestWindowFeature(Window.FEATURE_NO_TITLE); //不要标题 86 requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); /*先给Activity注册*/ 87 88 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); // 全屏显示 89 90 setContentView(R.layout.show_app_grid); 91 setProgressBarIndeterminateVisibility(true); // 92 93 AnimationSet set = new AnimationSet(false); 94 Animation animation = new AlphaAnimation(0,1); 95 animation.setDuration(500); 96 set.addAnimation(animation); 97 98 animation = new TranslateAnimation(1, 13, 10, 50); 99 animation.setDuration(300); 100 set.addAnimation(animation); 101 102 animation = new RotateAnimation(30,10); 103 animation.setDuration(300); 104 set.addAnimation(animation); 105 106 animation = new ScaleAnimation(5,0,2,0); //放大5倍, 放大2倍 大于0是放大 小于0是压缩 107 animation.setDuration(300); 108 set.addAnimation(animation); 109 110 LayoutAnimationController controller = new LayoutAnimationController(set, 1); 111 112 gv = (GridView) this.findViewById(R.id.gv_apps); 113 114 lv = (ListView) this.findViewById(R.id.lv_apps); 115 lv.setLayoutAnimation(controller); 116 lv.setCacheColorHint(0); 117 118 gv.setOnItemClickListener(this); /*点击程序图标有弹出信息*/ 119 lv.setOnItemClickListener(this); 120 121 ib_change_category = (ImageButton) this.findViewById(R.id.ib_change_category); 122 ib_change_view = (ImageButton) this.findViewById(R.id.ib_change_view); 123 ib_change_view.setOnClickListener(new OnClickListener() { 124 125 public void onClick(View v) { 126 if (isListView) { 127 128 MyToast.myToastShow(ShowAppActivity.this, R.drawable.grids, "网格显示", Toast.LENGTH_SHORT); 129 ib_change_view.setImageResource(R.drawable.grids); 130 lv.setVisibility(View.GONE); 131 gv.setVisibility(View.VISIBLE); 132 133 //AliphaAnimation 控制渐变透明的动画效果 134 //ScaleAnimation 控制尺寸伸缩的动画效果 135 //TranslateAnimation 控制画面平移的动画效果 136 //RotateAnimation 控制画面角度变化的动画效果 137 138 //LayoutAnimation 渲染ViewGroup中设置LayoutAnimation,为每一个view显示时候的动画效果 139 140 AnimationSet set = new AnimationSet(false); 141 Animation animation = new RotateAnimation(60,0); //先加速后减速 142 animation.setInterpolator(ShowAppActivity.this, android.R.anim.overshoot_interpolator); //加速 accelerate_interpolator 143 animation.setDuration(1000); //overshoot 就是超速over 到边界,然后回退回来 144 set.addAnimation(animation); //bounce_interpolator 反弹 会使碰到边框就反弹振动一下。 145 146 animation = new AlphaAnimation(0, 1); //控制透明度 0是代表完全透明。 1是代表一点都不透明 147 animation.setDuration(500); 148 set.addAnimation(animation); 149 gv.startAnimation(set); 150 151 //Animation animation = AnimationUtils.loadAnimation(ShowAppActivity.this, R.anim.set1); 152 //gv.startAnimation(animation); 153 154 isListView = false; 155 } else { 156 157 MyToast.myToastShow(ShowAppActivity.this, R.drawable.list, "列表显示", Toast.LENGTH_SHORT); 158 159 ib_change_view.setImageResource(R.drawable.list); 160 gv.setVisibility(View.GONE); 161 lv.setVisibility(View.VISIBLE); 162 AnimationSet set = new AnimationSet(false); 163 Animation animation = new TranslateAnimation (200, 1, 130, 1); 164 animation.setDuration(500); 165 //animation.setInterpolator(ShowAppActivity.this, android.R.anim.bounce_interpolator); 166 set.addAnimation(animation); //bounce 反弹 167 animation = new ScaleAnimation(0,1,0,1); //控制尺寸伸缩的动画效果 168 169 animation.setDuration(500); 170 set.addAnimation(animation); 171 172 //Animation animation = AnimationUtils.loadAnimation(ShowAppActivity.this, R.anim.set2); 173 174 175 lv.startAnimation(set); 176 isListView = true; 177 } 178 } 179 }); 180 ib_change_category.setOnClickListener(new OnClickListener() { 181 public void onClick(View v) { 182 if (allApplication) { 183 ib_change_category.setImageResource(R.drawable.user); 184 185 showPackageInfos = userPackageInfos; 186 allApplication = false; 187 188 MyToast.myToastShow(ShowAppActivity.this, R.drawable.user, "用户安装的程序列表",Toast.LENGTH_SHORT); 189 } else { 190 ib_change_category.setImageResource(R.drawable.all); 191 // gv.setAdapter(new 192 // GridViewAdapter(ShowAppActivity.this,packageInfos)); 193 showPackageInfos = packageInfos; 194 allApplication = true; 195 //Toast.makeText(ShowAppActivity.this, "所有程序列表",Toast.LENGTH_SHORT).show(); 196 MyToast.myToastShow(ShowAppActivity.this, R.drawable.all, "所有程序列表",Toast.LENGTH_SHORT); 197 } 198 gv.setAdapter(new GridViewAdapter(ShowAppActivity.this,showPackageInfos)); 199 lv.setAdapter(new ListViewAdapter(ShowAppActivity.this,showPackageInfos)); 200 201 } 202 }); 203 pd = ProgressDialog.show(this, "请稍候...", "正在搜索你所安装的应用程序...", true,false); 204 Thread t = new Thread(this); 205 t.start(); 206 } 207 208 class GridViewAdapter extends BaseAdapter { // 209 LayoutInflater inflater; 210 211 List<PackageInfo> pkInfos; 212 213 public GridViewAdapter(Context context, List<PackageInfo> packageInfos) { 214 inflater = LayoutInflater.from(context); 215 this.pkInfos = packageInfos; 216 } 217 218 public int getCount() { 219 220 return pkInfos.size(); // 221 } 222 223 @Override 224 public Object getItem(int arg0) { 225 return pkInfos.get(arg0); 226 } 227 228 @Override 229 public long getItemId(int position) { 230 231 return position; 232 } 233 234 @Override 235 public View getView(int position, View convertView, ViewGroup parent) { 236 View view = inflater.inflate(R.layout.gv_item, null); // 237 TextView tv = (TextView) view.findViewById(R.id.gv_item_appname); 238 ImageView iv = (ImageView) view.findViewById(R.id.gv_item_icon); 239 240 // tv.setText(packageInfos.get(position).packageName); // 241 tv.setText(pkInfos.get(position).applicationInfo.loadLabel(getPackageManager())); 242 243 iv.setImageDrawable(pkInfos.get(position).applicationInfo.loadIcon(getPackageManager())); 244 return view; 245 } 246 } 247 248 class ListViewAdapter extends BaseAdapter { // ListViewAdapter 249 LayoutInflater inflater; 250 List<PackageInfo> pkInfos; 251 252 public ListViewAdapter(Context context, List<PackageInfo> packageInfos) { 253 inflater = LayoutInflater.from(context); 254 this.pkInfos = packageInfos; 255 } 256 257 public int getCount() { 258 259 return pkInfos.size(); // 260 } 261 262 @Override 263 public Object getItem(int arg0) { 264 return pkInfos.get(arg0); 265 } 266 267 @Override 268 public long getItemId(int position) { 269 270 return position; 271 } 272 273 @Override 274 public View getView(int position, View convertView, ViewGroup parent) { 275 View view = inflater.inflate(R.layout.lv_item, null); // 276 TextView tv_appname = (TextView) view.findViewById(R.id.lv_item_appname); 277 TextView tv_packagename = (TextView) view.findViewById(R.id.lv_item_packagename); 278 279 ImageView iv = (ImageView) view.findViewById(R.id.lv_icon); 280 281 tv_packagename.setText(packageInfos.get(position).packageName); // 282 tv_appname.setText(pkInfos.get(position).applicationInfo.loadLabel(getPackageManager())); 283 284 iv.setImageDrawable(pkInfos.get(position).applicationInfo.loadIcon(getPackageManager())); 285 return view; 286 } 287 288 } 289 290 @Override 291 public void run() { 292 packageInfos = getPackageManager().getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES | PackageManager.GET_ACTIVITIES); 293 userPackageInfos = new ArrayList<PackageInfo>(); 294 for (int i = 0; i < packageInfos.size(); i++) { 295 296 PackageInfo temp = packageInfos.get(i); 297 ApplicationInfo appInfo = temp.applicationInfo; 298 boolean flag = false; 299 if ((appInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) { 300 flag = true; 301 } else if ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) { 302 flag = true; 303 } 304 if (flag) { 305 userPackageInfos.add(temp); 306 } 307 } 308 try { 309 Thread.currentThread().sleep(2000); 310 } catch (InterruptedException e) { // 311 e.printStackTrace(); 312 } 313 mHandler.sendEmptyMessage(SEARCH_APP); 314 315 try { 316 Thread.currentThread().sleep(5000); 317 mHandler.sendEmptyMessage(DELETE_APP); 318 } catch (InterruptedException e) { 319 e.printStackTrace(); 320 } 321 } 322 323 @Override 324 public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) { 325 final PackageInfo tempPkInfo = showPackageInfos.get(position); 326 327 //new Dialog to choose a choice 328 //创建Dialog的构造器 329 AlertDialog.Builder builder = new AlertDialog.Builder(this); 330 builder.setTitle("选项"); 331 builder.setItems(R.array.choice, new DialogInterface.OnClickListener() { 332 333 @Override 334 public void onClick(DialogInterface dialog, int which) { 335 switch(which){ 336 case 0: 337 String packageName = tempPkInfo.packageName; 338 ActivityInfo activityInfo = tempPkInfo.activities[0]; 339 if(activityInfo == null){ 340 Toast.makeText(ShowAppActivity.this, "没有任何Activity", Toast.LENGTH_SHORT).show(); // 341 return; 342 } 343 String activityName = activityInfo.name; 344 Intent intent = new Intent(); 345 intent.setComponent(new ComponentName(packageName,activityName)); 346 startActivity(intent); //启动程序 347 break; 348 case 1: 349 showAppDetail(tempPkInfo); //详细信息 350 break; 351 case 2: 352 Uri packageUri = Uri.parse("package:" + tempPkInfo.packageName); 353 Intent deleteIntent = new Intent(); 354 deleteIntent.setAction(Intent.ACTION_DELETE); 355 deleteIntent.setData(packageUri); 356 startActivityForResult(deleteIntent,0); 357 break; 358 } 359 360 } 361 }); 362 builder.setNegativeButton("取消", null); 363 builder.create().show(); 364 365 } 366 367 368 369 @Override 370 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 371 // TODO Auto-generated method stub 372 super.onActivityResult(requestCode, resultCode, data); 373 packageInfos = getPackageManager().getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES | PackageManager.GET_ACTIVITIES); 374 375 userPackageInfos = new ArrayList<PackageInfo>(); 376 for (int i = 0; i < packageInfos.size(); i++) { 377 378 PackageInfo temp = packageInfos.get(i); 379 ApplicationInfo appInfo = temp.applicationInfo; 380 boolean flag = false; 381 if ((appInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) { 382 flag = true; 383 } else if ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) { 384 flag = true; 385 } 386 if (flag) { 387 userPackageInfos.add(temp); 388 } 389 } 390 391 if(allApplication){ 392 showPackageInfos = packageInfos; 393 }else{ 394 showPackageInfos = userPackageInfos; 395 } 396 397 gv.setAdapter(new GridViewAdapter(ShowAppActivity.this,showPackageInfos)); 398 lv.setAdapter(new ListViewAdapter(ShowAppActivity.this,showPackageInfos)); 399 } 400 public void onResume(){ 401 super.onResume(); 402 } 403 404 private void showAppDetail(PackageInfo packageInfo){ 405 406 AlertDialog.Builder builder = new AlertDialog.Builder(this); 407 builder.setTitle("详细信息"); 408 409 StringBuffer message = new StringBuffer(); 410 message.append("程序名称:"+ packageInfo.applicationInfo.loadLabel(getPackageManager())); 411 message.append("\n 包名:" + packageInfo.packageName); 412 message.append("\n 版本号:" + packageInfo.versionCode); 413 message.append("\n 版本名:" + packageInfo.versionName); 414 415 builder.setMessage(message.toString()); 416 builder.setIcon(packageInfo.applicationInfo.loadIcon(getPackageManager())); 417 builder.setPositiveButton("确定",null); 418 builder.create().show(); 419 } 420 }
补充 知识点:
Animation animation = new RotateAnimation(60,0); //60度的效果,见下图