Android四分之一屏实现
package com.example.oneoffourdemo; import android.app.ActivityGroup; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.util.DisplayMetrics; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.RelativeLayout.LayoutParams; /** * Android 四分之一屏演示示例 * @author xiaolifan 2012-10-25*/ public class MainActivity extends ActivityGroup implements OnClickListener{ private static int currentPage = 0; private LinearLayout mContentView = null; private LinearLayout mContentViewParent = null; /**判断四分之一屏是否已经打开*/ private boolean isOpen = true; /**屏幕宽度*/ private static int mSreenWidth = 0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); getScreenWidth(); } private void initView(){ findViewById(R.id.button1).setOnClickListener(this); findViewById(R.id.button2).setOnClickListener(this); findViewById(R.id.button3).setOnClickListener(this); findViewById(R.id.button4).setOnClickListener(this); mContentView = (LinearLayout) findViewById(R.id.content_layout); mContentViewParent = (LinearLayout)findViewById(R.id.content_layout_parent); } /**获取屏幕宽度*/ private void getScreenWidth(){ DisplayMetrics metric = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metric); mSreenWidth = metric.widthPixels; // 屏幕宽度(像素) } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } @Override public void onClick(View arg0) { switch (arg0.getId()) { case R.id.button1: openRightWindow("1", 1); break; case R.id.button2: openRightWindow("2", 2); break; case R.id.button3: openLeftPanle(); break; case R.id.button4: break; } } private void openLeftPanle(){ if (!isOpen) { openRightWindow(String.valueOf(currentPage), currentPage); } else { RelativeLayout.LayoutParams lp = (LayoutParams) mContentViewParent.getLayoutParams(); int margin = mSreenWidth - dip2px(this, 54); lp.leftMargin = margin; lp.rightMargin = -margin; mContentViewParent.setLayoutParams(lp); isOpen = false; } } private void openRightWindow(String Id, int which){ if (which != currentPage) { currentPage = which; Intent intent = new Intent(); switch (which) { case 1: intent.setClass(this, SecondActivity.class); break; case 2: intent.setClass(this, ThirdActivity.class); break; } View view = null; Window window = getLocalActivityManager().startActivity(Id, intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)); if (window != null) { view = window.getDecorView(); } mContentView.removeAllViews(); mContentView.addView(view, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)); } RelativeLayout.LayoutParams lp = (LayoutParams) mContentViewParent.getLayoutParams(); lp.leftMargin = 0; lp.rightMargin = 0; mContentViewParent.setLayoutParams(lp); isOpen = true; } /** * 根据手机的分辨率从 dp 的单位 转成为 px(像素) */ public static int dip2px(Context context, float dpValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } /** * 根据手机的分辨率从 px(像素) 的单位 转成为 dp */ public static int px2dip(Context context, float pxValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (pxValue / scale + 0.5f); } }