Android常用控件的应用

关于Android常用控件 图片框与进度条的应用
案例一:消息提示框
1. Toast(吐丝框)
1.1 Toast是Android中的一种简易的消息提示框
1.2 使用这个类的最简单的方法是调用静态方法构造您所需要的一切,并返回一个新的Toast对象。
Toast toast=Toast.makeText(getApplicationContext(), “默认的Toast”, Toast.LENGTH_SHORT);
toast.show();
第一个参数:当前的上下文环境。可用getApplicationContext()或this
第二个参数:要显示的字符串。也可是R.string中字符串ID
第三个参数:显示的时间长短。Toast默认的有两个LENGTH_LONG(长)和LENGTH_SHORT(短),也可以使用毫秒如2000ms

  1. ImageView
    2.0 注意事项:res下面的资源名不能使用大写字母,特别是图片资源中的图片的文件名,会造成R.java文件丢失,改正确后即可
    例如:smallImage.png错误,small_image.png正确

    2.1 scaleType(缩放类型)
    scaleType的属性值有:matrix fitXY fitStart fitCenter fitEnd center centerCrop centerInside

            (扩大或缩小至)
    图片宽度--------------->控件宽度        
    

    它们之间的区别如下:
    matrix 用矩阵来绘制(从左上角起始的矩阵区域)

    fitXY 不按比例缩放图片,目标是把整个图片塞满整个View

    fitStart 把图片按比例扩大或缩小到View的[[[宽度]]],然后置顶部显示(图片会完整显示)

    fitCenter 把图片按比例扩大或缩小到View的[[[宽度]]],然后居中显示(图片会完整显示)

    fitEnd 把图片按比例扩大或缩小到View的[[[宽度]]],然后置底部显示(图片会完整显示)

    center 不缩放,将图片按原来大小居中显示,当图片宽高超过View的宽高时,则截取图片的居中部分显示

    centerCrop 按比例扩大(或缩小)图片的size居中显示,使得图片的长宽的[[[等于或大于]]]View的长宽

    centerInside 按比例扩大(或缩小)图片的size居中显示,使得图片的长宽的[[[等于或小于]]]View的长宽 (图片会完整显示)

    扫描二维码关注公众号,回复: 3380085 查看本文章
    用得最多还是fitXY fitStart fitCenter fitEnd
    默认是fitCenter
    

    scaleType属性分二种情况分析
    1.1.1 图片比ImageView小(android08_widget03_d01_v1)

package com.example.android01;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Switch;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private ProgressBar pb_main_download;
    private TextView tv_main_desc;
    private int p = 0;
    private MyHandler myHandler = new MyHandler();
    public class MyHandler extends Handler{
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            int code = msg.what;
            switch (code){
                case 1:
                    p++;
                    pb_main_download.setProgress(p);
                    tv_main_desc.setText(p+"%");
                    break;
                case 2:
                    break;
            }
        }
    }

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

        pb_main_download = findViewById(R.id.pb_main_download);
        tv_main_desc = findViewById(R.id.tv_main_desc);
    }


    public void download(View view) {
        if(0==p){
            new MyThread().start();
        }
    }

    public class MyThread extends Thread{
        @Override
        public void run() {
            super.run();
            while (true){
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if(p == 100){
                    p = 0;
                    break;
                }

                Message msg = new Message();
                msg.what = 1;
                myHandler.sendMessage(msg);

            }
        }
    }
}

以下是xml代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="60dp">
        <TextView
            android:layout_width="80dp"
            android:textColor="@color/red"
            android:id="@+id/tv_main_desc"
            android:textSize="30dp"
            android:layout_height="match_parent" />

        <ProgressBar
            android:layout_width="match_parent"
            style="?android:attr/progressBarStyleHorizontal"
            android:id="@+id/pb_main_download"
            android:max="100"
            android:layout_height="match_parent" />

    </FrameLayout>


    <Button
        android:layout_width="match_parent"
        android:text="Download"
        android:onClick="download"
        android:layout_height="wrap_content" />



</LinearLayout>
  线程休眠
  Thread.sleep(100);//抛异常
  SystemClock.sleep(100);//不会抛异常

3.2 线程注意事项:
不能在主线程中执行耗时的操作,只能在子线程中操作
另外,在子线程中不能操作主线程中的控件(ProgressBar除外)

 SubThread->MainThread    错误

3.3 Handler(重点、面试问得比较多)
用于线程之间的通信,比如:主线程与子线程

3.4 线程小结
SubThread->MainThread 错误
SubThread->Handler->MainThread 正确

  核心:
  1、toast弹框
  2、imageview
  3、进度条
  4、子线程访问主线程(线程中的通信handler)

猜你喜欢

转载自blog.csdn.net/zpl_118/article/details/82731368
今日推荐