Android学习笔记⑥——UI组件的学习ImageView相关

ImageView是集成了View的组件,它的主要工作就是显示一些图片啊,虽然他的用法一句话概括了,但是我觉得学起来应该不会太简单,正所谓 短小而精悍么 :) 

ImageView 派生了 ImageButton与QuickContactBadge两个类,ImageButton 与 Button的区别在于Button生成的按钮显示的文字,而ImageButton上面显示的是图片,当然我们也可以给Button设置他的background来添加一些图片;ImageButton是非常灵活的一个组件,因为他可以根据自定义的Drawable来改变按钮的一些动态样式;ImageButton同时也派生了一个ZoomButton得类,他可以“放大”、“缩小”跟他相似的还有一个叫做 ZoomControls的组件,只不过他同时提供了放大跟缩小的功能。

下面做一个例子,做一个简单的相册,上同时可以改变他的透明度

布局代码:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical">
 6 
 7     <LinearLayout
 8         android:layout_width="wrap_content"
 9         android:layout_height="wrap_content"
10         android:layout_gravity="center_horizontal">
11 
12         <Button
13             android:id="@+id/btnDown"
14             android:layout_width="wrap_content"
15             android:layout_height="wrap_content"
16             android:text="降低透明底" />
17 
18         <Button
19             android:id="@+id/btnUp"
20             android:layout_width="wrap_content"
21             android:layout_height="wrap_content"
22             android:text="增加透明底" />
23 
24         <Button
25             android:id="@+id/btnNext"
26             android:layout_width="wrap_content"
27             android:layout_height="wrap_content"
28             android:text="下一张图片" />
29     </LinearLayout>
30 
31     <LinearLayout
32         android:layout_width="match_parent"
33         android:layout_height="wrap_content">
34 
35         <ImageView
36             android:id="@+id/image"
37             android:layout_width="match_parent"
38             android:layout_height="wrap_content"
39             android:scaleType="fitCenter"
40             android:src="@drawable/pic1" />
41     </LinearLayout>
42 
43 </LinearLayout>

java代码:

 1 package com.doliao.helloworld;
 2 
 3 import android.os.Bundle;
 4 import android.support.v7.app.AppCompatActivity;
 5 import android.view.View;
 6 import android.widget.Button;
 7 import android.widget.ImageView;
 8 
 9 public class MainActivity extends AppCompatActivity implements View.OnClickListener {
10 
11     ImageView imageView;
12     Button btnUp, btnDown, btnNext;
13     int arrayImageId[] = new int[]{R.drawable.pic1, R.drawable.pic2, R.drawable.pic3, R.drawable.pic4, R.drawable.pic5};
14     int currentIma = 1;  //默认为第一张
15     int alpha = 255; //默认alpha值为255
16 
17     @Override
18     protected void onCreate(Bundle savedInstanceState) {
19         super.onCreate(savedInstanceState);
20         setContentView(R.layout.activity_imageview);
21 
22         imageView = (ImageView) findViewById(R.id.image);
23         btnUp = (Button) findViewById(R.id.btnUp);
24         btnDown = (Button) findViewById(R.id.btnDown);
25         btnNext = (Button) findViewById(R.id.btnNext);
26 
27         btnUp.setOnClickListener(this);
28         btnDown.setOnClickListener(this);
29         btnNext.setOnClickListener(this);
30 
31     }
32 
33     @Override
34     public void onClick(View v) {
35         if (v.equals(btnNext)) {
36             imageView.setImageResource(arrayImageId[++currentIma % arrayImageId.length]);
37         }
38         if (v.equals(btnUp)) {
39             alpha += 20;
40             if (alpha >= 255) {
41                 alpha = 255;
42             }
43         }
44         if (v.equals(btnDown)) {
45             alpha -= 20;
46             if (alpha <= 0) {
47                 alpha = 0;
48             }
49         }
50         imageView.setImageAlpha(alpha);
51     }
52 }

下面是运行的图:

ImageButton 与Button 同为按钮组件,下面我们就来粗略的看一下他们的相同点和不同点

因而ImageButton 不支持setText,而Button支持。反之,ImageButton 支持setImageURI,而Button不支持

下图是Button ImageButton的图,可以看出 一个咋点击、弹起有个不同的形态,还是一个是一个形态,默认相同。

 

 

QuickContactBadge(关联联系人)

QuickContactBadge说白了也是图片按钮,但是他与图片按钮不同的就是,他可以指定联系人,当用户单击图片的时候就可以联系指定的人了。

下面简单的学习下例子,用教程来记忆这个关键词。

布局内的代码为:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical" android:layout_width="match_parent"
 4     android:layout_height="match_parent">
 5 
 6     <QuickContactBadge
 7         android:id="@+id/badge"
 8         android:layout_width="wrap_content"
 9         android:layout_height="wrap_content"
10         android:src="@drawable/pic1"
11         />
12 </LinearLayout>

java内的代码:

 1 package com.doliao.helloworld;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 import android.widget.QuickContactBadge;
 6 
 7 /**
 8  * Created by Administrator on 2016/10/10 0010.
 9  */
10 
11 public class ImageViewActivity extends Activity{
12 
13     QuickContactBadge badge;
14     @Override
15     protected void onCreate(Bundle savedInstanceState) {
16         super.onCreate(savedInstanceState);
17         setContentView(R.layout.activyty_imageview2);
18         badge = (QuickContactBadge) findViewById(R.id.badge);
19 
20         badge.assignContactFromEmail("021-88888888",false);
21     }
22 }

运行的图片如下:

 

这写就是ImageView的一些常用的组件已经常用的方法,如果错误,请指出,谢谢!!

当然这不是ImageView下的所以的组件,还有其他的没有涉及到,等以后做东西遇到了在补充。

 

posted @ 2016-10-10 09:28  大园子  阅读(416)  评论(0)    收藏  举报