編輯:關於Android編程
閒來無事,總結了兩個自定義的ProgressDialog,大家可以參考下,根據自己需要進行選擇修改:
實現效果:
示例1:
示例2:
代碼如下:
MainActivity:只是兩個Button點擊事件
package com.customwaitdialog; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity implements OnClickListener { private Button btn_customDialog1; private Button btn_customDialog2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { btn_customDialog1 = (Button) findViewById(R.id.btn_customDialog1); btn_customDialog2 = (Button) findViewById(R.id.btn_customDialog2); btn_customDialog1.setOnClickListener(this); btn_customDialog2.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { // 示例1 case R.id.btn_customDialog1: Intent intent1 = new Intent(this, DialogActivity1.class); startActivity(intent1); break; // 示例2 case R.id.btn_customDialog2: Intent intent2 = new Intent(this, DialogActivity2.class); startActivity(intent2); break; default: break; } } }
package com.customwaitdialog; import android.app.Activity; import android.os.AsyncTask; import android.os.Bundle; import com.dialogutil.CustomWaitDialog1; public class DialogActivity1 extends Activity { private MainFrameTask mMainFrameTask = null; private CustomWaitDialog1 progressDialog = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity1); mMainFrameTask = new MainFrameTask(this); mMainFrameTask.execute(); } @Override protected void onDestroy() { stopProgressDialog(); if (mMainFrameTask != null && !mMainFrameTask.isCancelled()) { mMainFrameTask.cancel(true); } super.onDestroy(); } private void startProgressDialog() { if (progressDialog == null) { progressDialog = CustomWaitDialog1.createDialog(this); progressDialog.setMessage(正在加載中...); } progressDialog.show(); } private void stopProgressDialog() { if (progressDialog != null) { progressDialog.dismiss(); progressDialog = null; } } public class MainFrameTask extends AsyncTask{ private DialogActivity1 mainFrame = null; public MainFrameTask(DialogActivity1 mainFrame) { this.mainFrame = mainFrame; } @Override protected void onCancelled() { stopProgressDialog(); super.onCancelled(); } @Override protected Integer doInBackground(Integer... params) { try { Thread.sleep(10 * 1000); } catch (InterruptedException e) { e.printStackTrace(); } return null; } @Override protected void onPreExecute() { startProgressDialog(); } @Override protected void onPostExecute(Integer result) { stopProgressDialog(); } } }
重點來了CustomWaitDialog1:
package com.dialogutil; import android.app.Dialog; import android.content.Context; import android.graphics.drawable.AnimationDrawable; import android.view.Gravity; import android.widget.ImageView; import android.widget.TextView; import com.customwaitdialog.R; public class CustomWaitDialog1 extends Dialog { private Context context = null; private static CustomWaitDialog1 customProgressDialog = null; public CustomWaitDialog1(Context context) { super(context); this.context = context; } public CustomWaitDialog1(Context context, int theme) { super(context, theme); } public static CustomWaitDialog1 createDialog(Context context) { customProgressDialog = new CustomWaitDialog1(context, R.style.CustomProgressDialog); customProgressDialog.setCanceledOnTouchOutside(false); customProgressDialog.setContentView(R.layout.customprogressdialog); customProgressDialog.getWindow().getAttributes().gravity = Gravity.CENTER; return customProgressDialog; } public void onWindowFocusChanged(boolean hasFocus) { if (customProgressDialog == null) { return; } ImageView imageView = (ImageView) customProgressDialog .findViewById(R.id.loadingImageView); AnimationDrawable animationDrawable = (AnimationDrawable) imageView .getBackground(); animationDrawable.start(); } public CustomWaitDialog1 setTitile(String strTitle){ return customProgressDialog; } public CustomWaitDialog1 setMessage(String strMessage){ TextView tvMsg = (TextView)customProgressDialog.findViewById(R.id.id_tv_loadingmsg); if (tvMsg != null){ tvMsg.setText(strMessage); } return customProgressDialog; } }
style:
布局customprogressdialog.xml:
示例2DialogActivity2:
package com.customwaitdialog; import android.app.Activity; import android.content.DialogInterface; import android.content.DialogInterface.OnDismissListener; import android.os.Bundle; import com.dialogutil.CustomWaitDialog2; public class DialogActivity2 extends Activity { private CustomWaitDialog2 waitDialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity2); waitDialog = new CustomWaitDialog2(this); waitDialog.show(); waitDialog.setOnDismissListener(new OnDismissListener() { @Override public void onDismiss(DialogInterface dialog) { } }); } }
package com.dialogutil; import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; import android.view.Window; import android.view.WindowManager; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.ImageView; import com.customwaitdialog.R; public class CustomWaitDialog2 { private Context mContext; private ImageView image; private ImageView loading_pic_bigView; private Dialog waitDialog; private Animation mAnimation; public CustomWaitDialog2(Context mContext) { this.mContext = mContext; waitDialog = new Dialog(mContext, R.style.common_dialog); waitDialog.setContentView(R.layout.loading); waitDialog.setCanceledOnTouchOutside(false); /** * 設置幕布,也就是本dialog的背景層 dimAmount在0.0f和1.0f之間,0.0f完全不暗,即背景是可見的 * ,1.0f時候,背景全部變黑暗。 * * 如果要達到背景全部變暗的效果,需要設置 * dialog.getWindow().addFlags(WindowManager.LayoutParams * .FLAG_DIM_BEHIND); ,否則,背景無效果。 */ Window window = waitDialog.getWindow(); WindowManager.LayoutParams lp = window.getAttributes(); lp.dimAmount = 0.8f; window.setAttributes(lp); window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); // waitDialog.getWindow().getAttributes().gravity = Gravity.CENTER; /** * 設置透明度,主要設置的是dialog自身的透明度 */ loading_pic_bigView = (ImageView) waitDialog .findViewById(R.id.loading_pic_bigView); loading_pic_bigView.setAlpha(0.6f); image = (ImageView) waitDialog.findViewById(R.id.loading_pic_big); mAnimation = AnimationUtils.loadAnimation(mContext, R.anim.loading); } public void show() { image.startAnimation(mAnimation); waitDialog.show(); } public void dismiss() { waitDialog.dismiss(); } //用於網絡請求中斷操作 public void setOnDismissListener( DialogInterface.OnDismissListener dismissListener) { waitDialog.setOnDismissListener(dismissListener); } }
經常看一些大神的博客,大多數大神開篇都輕談一些國內比較專注的事和一些身邊瑣事,以表自己心情感悟。像我這種菜雞就直接步入正題吧。畢竟這東西就這麼簡單。Android動畫效果
前言雅虎天氣的界面上滑的時候背景圖片會跟著移動,最重要的是背景圖片會根據手指上下移動的距離來進行不同程度的模糊,感覺甚為驚奇,畢竟大家都知道,在Android平台上進行模
以前也實現過,ListView的上拉刷新,不過用的是開源代碼,由於本人比較懶吧,源碼也沒怎麼研究,所以現在寫出來還不是那麼流利。還好本人發現了自己的這些特點,所以寫出來個
在工作中又很多需求都不是android系統自帶的控件可以達到效果的,內置的TabHost就是,只能達到簡單的效果 ,所以這個時候就要自定義控件來達到效果:這個效果就是: