Android UIデザイン:ImageView

12559 ワード

ImageView
ImageViewは、インタフェースに画像を表示するためのコントロールです.
<ImageView  android:id="@+id/imageview" android:layout_width="match_parent" android:layout_height="wrap_content" />

属性:src:android:src="@mipmap/zly"//(画像が伸ばされない)画像を追加する2つの方法:使用方法1:layoutにsrc属性を追加android:src="@mipmap/zly"方法2: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(API 16)以降に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が1つ増えた背景だけです.これは、ImageButton自体がButtonボタンを設定し、それ自体が背景色を持っているからです.