Android UI设计:ImageView

ImageView

ImageView是用于在界面上展示图片的一个控件

<ImageView
           android:id="@+id/imageview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
/>

属性
srcandroid: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按钮,本身就有背景色。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值