Android(安卓)UI设计:ImageView
16lz
2022-06-02
ImageView
ImageView是用于在界面上展示图片的一个控件
<ImageView android:id="@+id/imageview" android:layout_width="match_parent" android:layout_height="wrap_content" />
属性:
src:android:src="@mipmap/zly"
//(图片不会被拉伸)
添加图片的两种方式:
使用方式
方式一:在layout中加入src属性
android:src="@mipmap/zly"
方式二:
在activity中通过id找到imageview
imageview.setImageViewResource(@mipmap/zly);
//()内为图片
background:android:background="@mipmap/zly"
//(会拉**伸)
scaleType:android:scaleType="centercrop"
//centercrop(充满窄边)
tint: android:tint="#33ff0000"
//蒙版覆盖
setImageAlpha:imageview.setImageAlpha(alphacount);
//(设置透明度)
Activity代码:
package com.kongjian.administrator.mykongjian;import android.annotation.TargetApi;import android.app.Activity;import android.media.Image;import android.os.Build;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.Button;import android.widget.ImageView;public class ImageActivity extends Activity implements View.OnClickListener{ private Button bt_alpha_add; private Button bt_alpha_sub; private ImageView imageview; private int alphacount=0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.baby); imageview= (ImageView) findViewById(R.id.imageview); bt_alpha_add= (Button) findViewById(R.id.bt_alpha_add); bt_alpha_sub= (Button) findViewById(R.id.bt_alpha_sub); bt_alpha_add.setOnClickListener(this); bt_alpha_sub.setOnClickListener(this); } @TargetApi(Build.VERSION_CODES.JELLY_BEAN) @Override public void onClick(View v) { int version= Build.VERSION.SDK_INT; switch (v.getId()){ case R.id.bt_alpha_add:{ alphacount+=5; if(version<16){ imageview.setAlpha(alphacount); }else{ imageview.setImageAlpha(alphacount); } } break; case R.id.bt_alpha_sub:{ alphacount-=5; if(version<16){ imageview.setAlpha(alphacount); }else{ imageview.setImageAlpha(alphacount); }; } break; default:break; } }}
注:以上代码中有android版本号的获得,这是因为在Android 4.1(API16)之后setAlpha()方法就被替换为了setImageAlpha()。
Layout代码:
<?xml version="1.0" encoding="utf-8"?><ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@mipmap/zly" /> <ImageView android:id="@+id/imageview" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@mipmap/zly" android:src="@mipmap/zzy" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/bt_alpha_add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="增加透明度"/> <Button android:id="@+id/bt_alpha_sub" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="减少透明度"/> </LinearLayout> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@mipmap/zly" android:scaleType="center"/> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@mipmap/zly" android:scaleType="centerInside"/> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@mipmap/zly" android:scaleType="fitStart"/> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@mipmap/zly" android:scaleType="fitEnd"/> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@mipmap/zly" android:scaleType="fitXY"/> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@mipmap/zly" android:scaleType="centerCrop"/> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@mipmap/zly" android:tint="#33ff0000" android:scaleType="fitCenter"/> </LinearLayout></ScrollView>
透明度效果图:自行测试
蒙版效果图:
ImageButton
ImageView与ImageButton的区别仅仅是ImageButton多了一个背景,这是因为ImageButton本身设一个Button按钮,本身就有背景色。
更多相关文章
- android常用开源库分享
- Android设置透明、半透明等效果
- 2011.09.13(3)——— android 添加快捷方式并且图标上添加数字
- Android设置透明、半透明等效果
- Android图片下载缓存库picasso解析
- APK 瘦身
- Android解析xml的Demo
- android背景选择器selector用法汇总
- Android(安卓)-- android activity 各种布局方式以及相关参数