layout :

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/FrameLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.viewoverlaptodrag.MainActivity" >

<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_launcher" />

<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />

</FrameLayout>

 

MainActivity :

public class MainActivity extends Activity implements OnTouchListener {

DisplayMetrics dm;
int lastX , lastY;
int ScreenWidth , ScreenHeight;
ImageButton MoveButton;
int densityDpi;
float density;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//隱藏 notification bar

getActionBarHeight();
getStatusBarHeight();
getHeight();

dm = getResources().getDisplayMetrics();
//取得螢幕顯示的資料
ScreenWidth = dm.widthPixels;
ScreenHeight = dm.heightPixels;
Log.d("ScreenWidth",""+ScreenWidth);
Log.d("ScreenHeight",""+ScreenHeight);
densityDpi = dm.densityDpi;
Log.d("densityDpi",""+densityDpi);
density = dm.density;
Log.d("density",""+density);
//int widthPix = (int) Math.ceil(dm.widthPixels * (dm.densityDpi / 160.0));
//int heightPix = (int) Math.ceil(dm.heightPixels * (dm.densityDpi / 160.0));
//Log.d("widthPix",""+widthPix);
//Log.d("heightPix",""+heightPix);
//螢幕寬和高的Pixels當作界線

MoveButton = (ImageButton) this.findViewById(R.id.imageButton1);
MoveButton.setOnTouchListener(this);

MoveButton.getBackground().setAlpha(0); // 0~255 透明~不透明
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

@Override
protected void onResume() {
// TODO Auto-generated method stub

getHeight();

super.onResume();
}

@Override
public boolean onTouch(View view, MotionEvent event) {
// TODO Auto-generated method stub

int events = event.getAction();

switch(events)
{
case MotionEvent.ACTION_DOWN:

lastX = (int)event.getRawX();
lastY = (int)event.getRawY();

break;

//layout(Left,Top,Right,Bottom)
// Left position, relative to parent
// Top position, relative to parent
// Right position, relative to parent
// Bottom position, relative to parent

case MotionEvent.ACTION_MOVE:

int Movex=(int)event.getRawX()-lastX;
int Movey=(int)event.getRawY()-lastY;

int Left=view.getLeft()+ Movex;
int Bottom=view.getBottom()+ Movey;
int Right=view.getRight()+ Movex;
int Top=view.getTop()+ Movey;

if(Left<0)
{
Left=0;
Right=Left+view.getWidth();
}

if(Top<0)
{
Top=0;
Bottom=Top+view.getHeight();
}

if(Right > ScreenWidth)
{
Right = ScreenWidth;
Left=Right-view.getWidth();
}

if(Bottom > ScreenHeight-192)
{
Bottom = ScreenHeight-192;
Top = Bottom - view.getHeight();
}

view.layout(Left, Top, Right, Bottom);
lastX =(int)event.getRawX();
lastY =(int)event.getRawY();
view.postInvalidate();

break;

}

return false;

}

public int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
Log.d("getStatusBarHeight",""+result);
return result;
}

public int getActionBarHeight() {
// Calculate ActionBar height
int result = 0;
TypedValue tv = new TypedValue();
if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
{
result = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
}
Log.d("getActionBarHeight",""+result);
return result;
}

public void getHeight(){
Rect rectangle= new Rect();
Window window = getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
int statusBarHeight= rectangle.top;
int contentViewTop=
window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
int titleBarHeight= contentViewTop - statusBarHeight;

Log.i("getHeight", "StatusBar Height= " + statusBarHeight + " , TitleBar Height = " + titleBarHeight);
}
}

 

 

 

arrow
arrow
    全站熱搜

    小彬彬 發表在 痞客邦 留言(0) 人氣()