天天看點

android手機使用者界面小執行個體

1、擷取手機螢幕分辨率

final DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
System.out.println("手機分辨率:" + dm.widthPixels + "--" + dm.heightPixels);      

2、控件跟随手指移動

首先在xml檔案中對頁面進行布局,在這裡我們放置一個按鈕,通過手指的移動來改變按鈕的位置

1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:id="@+id/activity_02"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     tools:context="com.example.whs.sample04.Activity02">
 8 
 9     <Button
10         android:id="@+id/moveBtn"
11         android:layout_width="wrap_content"
12         android:layout_height="wrap_content"
13         android:text="測試"
14         />
15 
16 </RelativeLayout>      

在Activity中進行具體的實作:

1 package com.example.whs.sample04;
 2 
 3 import android.support.v7.app.AppCompatActivity;
 4 import android.os.Bundle;
 5 import android.support.v7.widget.LinearLayoutCompat;
 6 import android.view.MotionEvent;
 7 import android.view.View;
 8 import android.view.ViewGroup;
 9 import android.widget.Button;
10 import android.widget.LinearLayout;
11 import android.widget.RelativeLayout;
12 
13 //實作按鈕的界面響應
14 public class Activity02 extends AppCompatActivity {
15 
16     int xSpan;  //在觸控筆點選按鈕的情況下相對于按鈕自己坐标系的值
17     int ySpan;
18     private ViewGroup root;
19 
20     Button btn;
21 
22     @Override
23     protected void onCreate(Bundle savedInstanceState) {
24         super.onCreate(savedInstanceState);
25         setContentView(R.layout.activity_02);
26 
27         root = (ViewGroup) findViewById(R.id.activity_02);
28         btn = (Button)findViewById(R.id.moveBtn);
29         btn.setOnTouchListener(new View.OnTouchListener() {
30             @Override
31             public boolean onTouch(View view, MotionEvent motionEvent) {
32                 switch (motionEvent.getAction()){
33                     case MotionEvent.ACTION_DOWN://按下
34                         xSpan = (int)motionEvent.getRawX();
35                         ySpan = (int)motionEvent.getRawY();
36                         break;
37                     case MotionEvent.ACTION_MOVE://移動
38 
39                         //擷取手指移動到了哪個點坐标
40                         int xMove = (int)motionEvent.getRawX();
41                         int yMove = (int)motionEvent.getRawY();
42                         //相對于上一個點,手指在x,y方向上分别移動的距離
43                         int dx = xMove - xSpan;
44                         int dy = yMove - ySpan;
45                         //擷取Button上一次各個邊距離父控件的距離
46                         int left = view.getLeft();
47                         int right = view.getRight();
48                         int bottom = view.getBottom();
49                         int top = view.getTop();
50                         //設定本次Button的上 下 左 右各邊與父控件的距離
51                         view.layout(left + dx,top + dy,right + dx,bottom + dy);
52                         // 本次移動的結尾作為下一次移動的開始
53                         xSpan = (int)motionEvent.getRawX();
54                         ySpan = (int)motionEvent.getRawY();
55                         break;
56                     case MotionEvent.ACTION_UP:
57                         break;
58                 }
59                 root.invalidate();
60                 return true;//如果傳回true,從手指接觸螢幕到手指離開螢幕,将不會觸發點選事件。
61             }
62         });
63 
64     }
65 
66 }      

View Code

 3、更改手機螢幕顯示方向

1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:id="@+id/activity_15"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     android:orientation="vertical"
 8     tools:context="com.example.whs.sample04.Activity15">
 9 
10     <RadioGroup
11         android:id="@+id/RadioGroup01"
12         android:layout_width="match_parent"
13         android:layout_height="wrap_content"
14         android:orientation="horizontal"
15         >
16         <RadioButton
17             android:id="@+id/radioButton01"
18             android:text="豎屏"
19             android:layout_width="wrap_content"
20             android:layout_height="wrap_content" />
21         <RadioButton
22             android:id="@+id/radioButton02"
23             android:text="橫屏"
24             android:layout_width="wrap_content"
25             android:layout_height="wrap_content" />
26 
27 
28     </RadioGroup>
29 
30 
31 
32 </LinearLayout>      

主要通過android.app.activity.getRequestedOrientation()來感覺螢幕的狀态,若傳回-1則無法識别螢幕狀态。用setRequestedOrientation來設定螢幕狀态。

1 package com.example.whs.sample04;
 2 
 3 import android.content.pm.ActivityInfo;
 4 import android.graphics.Color;
 5 import android.support.v7.app.AppCompatActivity;
 6 import android.os.Bundle;
 7 import android.widget.RadioButton;
 8 import android.widget.RadioGroup;
 9 import android.widget.Toast;
10 
11 //更改手機螢幕顯示方向
12 public class Activity15 extends AppCompatActivity {
13 
14     @Override
15     protected void onCreate(Bundle savedInstanceState) {
16         super.onCreate(savedInstanceState);
17 
18         setContentView(R.layout.activity_15);
19 
20         RadioGroup rg = (RadioGroup)findViewById(R.id.RadioGroup01);
21         final RadioButton rbH = (RadioButton)findViewById(R.id.radioButton01);
22         final RadioButton rbV = (RadioButton)findViewById(R.id.radioButton02);
23         // 取得目前螢幕方向
24         final int orient = getRequestedOrientation();
25         if (orient == -1){
26             Toast.makeText(this, "無法區分橫豎屏!!", Toast.LENGTH_SHORT).show();
27         }
28         System.out.println("螢幕:" + orient);
29         RadioGroup.OnCheckedChangeListener mChange = new RadioGroup.OnCheckedChangeListener() {
30             @Override
31             public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
32                 if (checkedId == rbH.getId()){//橫屏按鈕
33 
34                     if (orient == -1){
35                         Toast.makeText(Activity15.this, "無法區分橫豎屏!!", Toast.LENGTH_SHORT).show();
36                     }else {
37                         if (orient == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){//若為橫屏,設定為豎屏
38                             setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
39                             Toast.makeText(Activity15.this, "現在是豎屏啦!!", Toast.LENGTH_SHORT).show();
40                         }
41                     }
42 
43                 }else if (checkedId == rbV.getId()){//豎屏按鈕
44                     if (orient == -1){
45                         Toast.makeText(Activity15.this, "無法區分橫豎屏!!", Toast.LENGTH_SHORT).show();
46                     }else {
47                         if (orient == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){//若為豎屏,設定為橫屏
48                             setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
49                             Toast.makeText(Activity15.this, "現在是橫屏啦!!", Toast.LENGTH_SHORT).show();
50                         }
51                     }
52                 }
53             }
54         };
55         rg.setOnCheckedChangeListener(mChange); //RadioGroup添加監聽器
56 
57     }
58 }      

View Code

 3、擷取手機螢幕資訊

1         DisplayMetrics display = new DisplayMetrics();
 2         //将目前視窗的一些資訊放在DisplayMetrics類中,
 3         this.getWindowManager().getDefaultDisplay().getMetrics(display);
 6         float density = display.density;
 7         //輸出結果是 dens: density is  1.0
 8         Log.e("dens","density is  "+density);
 9 
10         //擷取螢幕像素密度
11         int densityDpi = display.densityDpi;
12         //輸出結果是 160
13         Log.e("dens","densityDpi is  "+densityDpi);
14 
15 
16         //擷取螢幕的高度 結果機關 px
17         int heightPixels = display.heightPixels;
18         //輸出結果是 heightPixels is  480
19         Log.e("dens","heightPixels is  "+heightPixels);
20 
21 
22         //擷取螢幕的寬度 結果機關 px
23         int widthPixels = display.widthPixels;
24         //輸出結果是 widthPixels is  320
25         Log.e("dens","widthPixels is  "+widthPixels);
26 
27 
28         //擷取縮放比例
29         float scaledDensity = display.scaledDensity;
30         //輸出結果是 scaledDensity is  1.0
31         Log.e("dens","scaledDensity is  "+scaledDensity);
32 
33         float xdpi = display.xdpi;
34         float ydpi = display.ydpi;
35 
36         //輸出結果是 xdpi is  160.0  ydpi 160.0
37         Log.e("dens","xdpi is  "+xdpi+"  ydpi "+ydpi);
38           
android手機使用者界面小執行個體

 4、動态改變控件的位置和大小

xml檔案中的布局如下:

1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout
 3         xmlns:android="http://schemas.android.com/apk/res/android"
 4         android:layout_width="match_parent"
 5         android:layout_height="match_parent"
 6         android:orientation="vertical"
 7         android:id="@+id/lls"
 8         >
 9         <TextView
10             android:id="@+id/textView_test"
11             android:layout_width="100dp"
12             android:layout_height="44dp"
13             android:background="#294881"
14             />
15 
16 
17 </LinearLayout>      
android手機使用者界面小執行個體

(1)動态改變TextView的大小和位置

1 TextView textView = (TextView)findViewById(R.id.textView_test);
2         LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) textView.getLayoutParams();
3         Log.e("dens", String.valueOf(params.width));
4         if (params != null){
5             params.width = (int)(120 * scaledDensity);
6             params.height = (int)(40 * scaledDensity);
7         }
8         //設定左邊距
9         params.setMargins((int)(20 * scaledDensity), (int)(20 * scaledDensity), 0, 0);      

(2)動态添加TextView到布局中

1         //動态建立控件
 2         LinearLayout mLlPrent = (LinearLayout)findViewById(R.id.lls);
 3         TextView textView1 = new TextView(this);
 4         //設定寬和高
 5         LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams((int)(120 * scaledDensity), (int)(40 * scaledDensity));
 6         //設定外邊距
 7         layoutParams.setMargins((int)(20 * scaledDensity), (int)(60 * scaledDensity), 0, 0);
 8         textView1.setPadding((int)(20 * scaledDensity), 0, 0, 0);
 9         textView1.setText("執行");
10         textView1.setLayoutParams(layoutParams);
11         textView1.setBackgroundColor(Color.GRAY);
12         //添加到布局檔案中
13         mLlPrent.addView(textView1);      

 4、視圖坐标

android手機使用者界面小執行個體
android手機使用者界面小執行個體