toast弹框、imageview、进度条、子线程访问主线程(线程中的通信handler)

  1、imageview
  
   ?xml version="1.0" encoding="utf-8"?>

<ImageView
android:layout_width=“200dp”
android:layout_marginLeft=“100dp”
android:src="@drawable/small_image" //不设置属性情况下的默认
android:scaleType=“matrix” //设置其图片位置属性
android:background="@color/colorPrimary"
android:layout_height=“100dp” />

1、不设置属性的默认情况 ![不设置属性的默认情况](https://img-blog.csdn.net/20181020182558766?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0tpZFNobWlseQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70) 2、多种图片属性的设置 ![设置图中任意属性改变图片 01.图比ImageView小 ](https://img-blog.csdn.net/20181020183028929?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0tpZFNobWlseQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70) ![02.图片比ImageView大](https://img-blog.csdn.net/20181020183321376?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0tpZFNobWlseQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)

1.2 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的长宽 (图片会完整显示)

    用得最多还是fitXY fitStart fitCenter fitEnd
    默认是fitCenter

  scaleType属性分二种情况分析
  1.1.1 图片比ImageView小(android08_widget03_d01_v1)
        <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginRight="10dp"
        android:background="#FF0000"
        android:scaleType="matrix"
        android:src="@drawable/small_image" />

  1.1.2 图片比ImageView大(android08_widget03_d01_v2)

2、进度条
进度条100%动态展示

----代码展示

<?xml version="1.0" encoding="utf-8"?>

<!--imageView 图片-->

<FrameLayout
    android:layout_width="match_parent"
    android:layout_margin="60dp"
    android:layout_height="80dp">

    <TextView
        android:layout_width="match_parent"
        android:id="@+id/tv_main_downloadShow"
        android:textSize="30dp"
        android:layout_marginLeft="200dp"
        android:layout_height="wrap_content" />

    <!-- 进度条-->
    <ProgressBar
        android:layout_width="match_parent"
        android:id="@+id/pd_main_download"
        android:layout_marginTop="10dp"
        style="?android:attr/progressBarStyleHorizontal"
        android:max="100"
        android:layout_height="20dp" />

</FrameLayout>


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

package com.example.myapplication06;

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

public class MainActivity extends AppCompatActivity {
private ProgressBar pd_main_download;
private TextView tv_main_downloadShow;
private int processCode = 0;
private Handler myHander = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 1:
processCode++;
tv_main_downloadShow.setText(processCode + “%”);
pd_main_download.setProgress(processCode);
break;
default:
break;
}
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    pd_main_download = findViewById(R.id.pd_main_download);
    tv_main_downloadShow = findViewById(R.id.tv_main_downloadShow);

}
//线程在安卓4.0版本后所有耗时操作都写而在了子线程里面

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


class MyThread extends Thread {
    @Override
    public void run() {
        super.run();
        while (true) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (processCode == 100) {
                processCode = 0;
                break;
            }
            Message msg = new Message();
            msg.what = 1;
            myHander.sendMessage(msg);
        }
    }
}

}

2.2 进度条 ProgressBar
2.2.1 常用属性
style="?android:attr/progressBarStyleHorizontal" 默认为圆形
android:progress=“33”
android:max=“100”

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

3.2.2 线程注意事项:
不能在主线程中执行耗时的操作,只能在子线程中操作
另外,在子线程中不能操作主线程中的控件(ProgressBar除外)
private class MyThread extends Thread{

 SubThread->MainThread    错误

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

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

猜你喜欢

转载自blog.csdn.net/KidShmily/article/details/83217240
今日推荐