• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

Android四分之一屏实现

Android 水墨上仙 2179次浏览

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);  
    } 
}


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明Android四分之一屏实现
喜欢 (0)
加载中……