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

时间:2022-10-15 16:33:32

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

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

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

布局代码:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"> <Button
android:id="@+id/btnDown"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="降低透明底" /> <Button
android:id="@+id/btnUp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="增加透明底" /> <Button
android:id="@+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下一张图片" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"> <ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:src="@drawable/pic1" />
</LinearLayout> </LinearLayout>

java代码:

 package com.doliao.helloworld;

 import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView; public class MainActivity extends AppCompatActivity implements View.OnClickListener { ImageView imageView;
Button btnUp, btnDown, btnNext;
int arrayImageId[] = new int[]{R.drawable.pic1, R.drawable.pic2, R.drawable.pic3, R.drawable.pic4, R.drawable.pic5};
int currentIma = 1; //默认为第一张
int alpha = 255; //默认alpha值为255 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_imageview); imageView = (ImageView) findViewById(R.id.image);
btnUp = (Button) findViewById(R.id.btnUp);
btnDown = (Button) findViewById(R.id.btnDown);
btnNext = (Button) findViewById(R.id.btnNext); btnUp.setOnClickListener(this);
btnDown.setOnClickListener(this);
btnNext.setOnClickListener(this); } @Override
public void onClick(View v) {
if (v.equals(btnNext)) {
imageView.setImageResource(arrayImageId[++currentIma % arrayImageId.length]);
}
if (v.equals(btnUp)) {
alpha += 20;
if (alpha >= 255) {
alpha = 255;
}
}
if (v.equals(btnDown)) {
alpha -= 20;
if (alpha <= 0) {
alpha = 0;
}
}
imageView.setImageAlpha(alpha);
}
}

下面是运行的图:

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

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

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

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

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

QuickContactBadge(关联联系人)

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

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

布局内的代码为:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"> <QuickContactBadge
android:id="@+id/badge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/pic1"
/>
</LinearLayout>

java内的代码:

 package com.doliao.helloworld;

 import android.app.Activity;
import android.os.Bundle;
import android.widget.QuickContactBadge; /**
* Created by Administrator on 2016/10/10 0010.
*/ public class ImageViewActivity extends Activity{ QuickContactBadge badge;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activyty_imageview2);
badge = (QuickContactBadge) findViewById(R.id.badge); badge.assignContactFromEmail("021-88888888",false);
}
}

运行的图片如下:

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

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

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