• 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     

     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>

    (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、视图坐标

  • 相关阅读:
    STL_算法_05_集合算法
    STL_算法_04_算术和生成算法
    STL_算法_03_拷贝和替换算法
    STL_算法_02_排序算法
    STL_算法_01_查找算法
    STL_容器使用时机
    STL_容器共通能力
    Qt5_选择文件对话框
    Qt5_当前exe所在路径
    Java 静态代理和动态代理
  • 原文地址:https://www.cnblogs.com/whongs/p/7456379.html
Copyright © 2020-2023  润新知