新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > Android系統(tǒng)開發(fā)全攻略(二)

Android系統(tǒng)開發(fā)全攻略(二)

作者: 時間:2012-06-08 來源:網(wǎng)絡(luò) 收藏

  public Panel(Context context,View otherView,int width,int height) {

  super(context);

  this.mContext=context;

  //改變Panel附近組件的屬性

  LayoutParams otherLP=(LayoutParams) otherView.getLayoutParams();

  otherLP.weight=1;//支持壓擠

  otherView.setLayoutParams(otherLP);

  //設(shè)置Panel本身的屬性

  LayoutParams lp=new LayoutParams(width, height);

  lp.rightMargin=-lp.width+HANDLE_WIDTH;//Panel的Container在屏幕不可視區(qū)域,Handle在可視區(qū)域

  mRightMargin=Math.abs(lp.rightMargin);

  this.setLayoutParams(lp);

  this.setOrientation(LinearLayout.HORIZONTAL);

  //設(shè)置Handle的屬性

  btnHandle=new Button(context);

  btnHandle.setLayoutParams(new LayoutParams(HANDLE_WIDTH,height));

  btnHandle.setOnClickListener(new OnClickListener(){

  @Override

  public void onClick(View arg0) {

  LayoutParams lp = (LayoutParams) Panel.this.getLayoutParams();

  if (lp.rightMargin 《 0)// CLOSE的狀態(tài)

  new AsynMove().execute(new Integer[] { MOVE_WIDTH });// 正數(shù)展開

  else if (lp.rightMargin 》= 0)// OPEN的狀態(tài)

  new AsynMove().execute(new Integer[] { -MOVE_WIDTH });// 負(fù)數(shù)收縮

  }

  });

  //btnHandle.setOnTouchListener(HandleTouchEvent);

  this.addView(btnHandle);

  //設(shè)置Container的屬性

  panelContainer=new LinearLayout(context);

  panelContainer.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,

  LayoutParams.FILL_PARENT));

  this.addView(panelContainer);

  }

  /**
  * 定義收縮時的回調(diào)函數(shù)

  * @param event

  */

  public void setPanelClosedEvent(PanelClosedEvent event)

  {

  this.panelClosedEvent=event;

  }

  /**

  * 定義展開時的回調(diào)函數(shù)

  * @param event

  */

  public void setPanelOpenedEvent(PanelOpenedEvent event)

  {

  this.panelOpenedEvent=event;

  }

  /**

  * 把View放在Panel的Container

  * @param v

  */

  public void fillPanelContainer(View v)

  {

  panelContainer.addView(v);

  }

/**

  * 異步移動Panel

  * @author hellogv

  */

  class AsynMove extends AsyncTask《Integer, Integer, Void》 {

  @Override

  protected Void doInBackground(Integer.。. params) {

  int times;

  if (mRightMargin % Math.abs(params[0]) == 0)// 整除

  times = mRightMargin / Math.abs(params[0]);

  else

  // 有余數(shù)

  times = mRightMargin / Math.abs(params[0]) + 1;

  for (int i = 0; i 《 times; i++) {

  publishProgress(params);

  try {

  Thread.sleep(Math.abs(params[0]));

  } catch (InterruptedException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  }

  }

  return null;

  }

  @Override

  protected void onProgressUpdate(Integer.。. params) {

  LayoutParams lp = (LayoutParams) Panel.this.getLayoutParams();

  if (params[0] 《 0)

  lp.rightMargin = Math.max(lp.rightMargin + params[0],

  (-mRightMargin));

  else

  lp.rightMargin = Math.min(lp.rightMargin + params[0], 0);

  if(lp.rightMargin==0 panelOpenedEvent!=null){//展開之后

  panelOpenedEvent.onPanelOpened(Panel.this);//調(diào)用OPEN回調(diào)函數(shù)

  }

  else if(lp.rightMargin==-(mRightMargin) panelClosedEvent!=null){//收縮之后

  panelClosedEvent.onPanelClosed(Panel.this);//調(diào)用CLOSE回調(diào)函數(shù)

  }

  Panel.this.setLayoutParams(lp);

  }

  }

  }

  《/span》

  《span style=“font-family:Comic Sans MS;font-size:18px;”》public class Panel extends LinearLayout{

  public interface PanelClosedEvent {

  void onPanelClosed(View panel);

  }

  public interface PanelOpenedEvent {

  void onPanelOpened(View panel);

  }

  /**Handle的寬度,與Panel等高*/

  private final static int HANDLE_WIDTH=30;

  /**每次自動展開/收縮的范圍*/

  private final static int MOVE_WIDTH=20;

  private Button btnHandle;

  private LinearLayout panelContainer;

  private int mRightMargin=0;

  private Context mContext;

  private PanelClosedEvent panelClosedEvent=null;

  private PanelOpenedEvent panelOpenedEvent=null;

  /**

  * otherView自動布局以適應(yīng)Panel展開/收縮的空間變化

  * @author GV

  *

  */

  public Panel(Context context,View otherView,int width,int height) {

  super(context);

  this.mContext=context;

  //改變Panel附近組件的屬性

  LayoutParams otherLP=(LayoutParams) otherView.getLayoutParams();

  otherLP.weight=1;//支持壓擠

  otherView.setLayoutParams(otherLP);

  //設(shè)置Panel本身的屬性

  LayoutParams lp=new LayoutParams(width, height);

  lp.rightMargin=-lp.width+HANDLE_WIDTH;//Panel的Container在屏幕不可視區(qū)域,Handle在可視區(qū)域

  mRightMargin=Math.abs(lp.rightMargin);

  this.setLayoutParams(lp);

  this.setOrientation(LinearLayout.HORIZONTAL);

  //設(shè)置Handle的屬性

  btnHandle=new Button(context);

  btnHandle.setLayoutParams(new LayoutParams(HANDLE_WIDTH,height));

  btnHandle.setOnClickListener(new OnClickListener(){

  @Override

  public void onClick(View arg0) {

  LayoutParams lp = (LayoutParams) Panel.this.getLayoutParams();

  if (lp.rightMargin 《 0)// CLOSE的狀態(tài)

  new AsynMove().execute(new Integer[] { MOVE_WIDTH });// 正數(shù)展開

  else if (lp.rightMargin 》= 0)// OPEN的狀態(tài)

  new AsynMove().execute(new Integer[] { -MOVE_WIDTH });// 負(fù)數(shù)收縮

  }

  });

  //btnHandle.setOnTouchListener(HandleTouchEvent);

  this.addView(btnHandle);

  //設(shè)置Container的屬性

  panelContainer=new LinearLayout(context);

  panelContainer.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,

  LayoutParams.FILL_PARENT));

  this.addView(panelContainer);

  }

  /**

  * 定義收縮時的回調(diào)函數(shù)

  * @param event

  */

  public void setPanelClosedEvent(PanelClosedEvent event)

  {

  this.panelClosedEvent=event;

  }

  /**

  * 定義展開時的回調(diào)函數(shù)

  * @param event

  */

  public void setPanelOpenedEvent(PanelOpenedEvent event)

  {

  this.panelOpenedEvent=event;

  }

  /**

  * 把View放在Panel的Container

  * @param v

  */

  public void fillPanelContainer(View v)

  {

  panelContainer.addView(v);

  }

  /**

  * 異步移動Panel

  * @author hellogv

  */

  class AsynMove extends AsyncTask《Integer, Integer, Void》 {

  @Override

  protected Void doInBackground(Integer.。. params) {

  int times;

  if (mRightMargin % Math.abs(params[0]) == 0)// 整除

  times = mRightMargin / Math.abs(params[0]);

  else

  // 有余數(shù)

  times = mRightMargin / Math.abs(params[0]) + 1;

  for (int i = 0; i 《 times; i++) {

  publishProgress(params);

  try {

  Thread.sleep(Math.abs(params[0]));

  } catch (InterruptedException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  }

  }

  return null;

  }

  @Override

  protected void onProgressUpdate(Integer.。. params) {

  LayoutParams lp = (LayoutParams) Panel.this.getLayoutParams();

  if (params[0] 《 0)

  lp.rightMargin = Math.max(lp.rightMargin + params[0],

 ?。?mRightMargin));

  else

  lp.rightMargin = Math.min(lp.rightMargin + params[0], 0);

  if(lp.rightMargin==0 panelOpenedEvent!=null){//展開之后

  panelOpenedEvent.onPanelOpened(Panel.this);//調(diào)用OPEN回調(diào)函數(shù)

  }

  else if(lp.rightMargin==-(mRightMargin) panelClosedEvent!=null){//收縮之后

  panelClosedEvent.onPanelClosed(Panel.this);//調(diào)用CLOSE回調(diào)函數(shù)

  }

  Panel.this.setLayoutParams(lp);

  }

  }

  }

  《/span》

  main.java是主控部分,演示了Panel的使用:

  [java] view plaincopyprint?

  《span style=“font-family:Comic Sans MS;font-size:18px;”》public class main extends Activity {

  public Panel panel;

  public LinearLayout container;

  public GridView gridview;

  public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.main);

  this.setTitle(““可動態(tài)布局”的抽屜組件之構(gòu)建基礎(chǔ)-----hellogv”);

  gridview = (GridView) findViewById(R.id.gridview);

  container=(LinearLayout)findViewById(R.id.container);

  panel=new Panel(this,gridview,200,LayoutParams.FILL_PARENT);

  container.addView(panel);//加入Panel控件

  //新建測試組件

  TextView tvTest=new TextView(this);

  tvTest.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));

  tvTest.setText(“測試組件,紅字白底”);

  tvTest.setTextColor(Color.RED);

  tvTest.setBackgroundColor(Color.WHITE);

  //加入到Panel里面

  panel.fillPanelContainer(tvTest);

  panel.setPanelClosedEvent(panelClosedEvent);

  panel.setPanelOpenedEvent(panelOpenedEvent);

  //往GridView填充測試數(shù)據(jù)

  ArrayList《HashMap《String, Object》》 lstImageItem = new ArrayList《HashMap《String, Object》》();

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

  HashMap《String, Object》 map = new HashMap《String, Object》();

  map.put(“ItemImage”, R.drawable.icon);

  map.put(“ItemText”, “NO.” + String.valueOf(i));

  lstImageItem.add(map);

  }

  SimpleAdapter saImageItems = new SimpleAdapter(this,

  lstImageItem,

  R.layout.item,

  new String[] { “ItemImage”, “ItemText” },

  new int[] { R.id.ItemImage, R.id.ItemText });

  gridview.setAdapter(saImageItems);

  gridview.setOnItemClickListener(new ItemClickListener());

  }

  PanelClosedEvent panelClosedEvent =new PanelClosedEvent(){

  @Override

  public void onPanelClosed(View panel) {

  Log.e(“panelClosedEvent”,“panelClosedEvent”);

  }

  };

  PanelOpenedEvent panelOpenedEvent =new PanelOpenedEvent(){

  @Override

  public void onPanelOpened(View panel) {

  Log.e(“panelOpenedEvent”,“panelOpenedEvent”);

  }

  };

  class ItemClickListener implements OnItemClickListener {

  @Override

  public void onItemClick(AdapterView《?》 arg0,View arg1, int arg2, long arg3) {

  @SuppressWarnings(“unchecked”)

  HashMap《String, Object》 item = (HashMap《String, Object》) arg0

  .getItemAtPosition(arg2);

  setTitle((String) item.get(“ItemText”));

  }

  }《/span》

  這次就在基礎(chǔ)篇的基礎(chǔ)上加入拖拉功能。拖拉功能基于GestureDetector,GestureDetector的基本使用方式不是本文介紹的重點(diǎn),有興趣的童鞋可以上網(wǎng)查詢相關(guān)的教程。

本文的抽屜控件相對于基礎(chǔ)篇的抽屜控件多了以下功能:

  1.支持手勢拖拉

  2.拖拉到一半時,可以自動展開或者收縮。

  具體如下圖:

  這次就在基礎(chǔ)篇的基礎(chǔ)上加入拖拉功能。拖拉功能基于GestureDetector,GestureDetector的基本使用方式

  本文的源碼可以到這里下載:http://download.csdn.net/detail/hellogv/3642418



評論


相關(guān)推薦

技術(shù)專區(qū)

關(guān)閉