zoukankan      html  css  js  c++  java
  • ImageView显示图像控件(基本用法)

        ImageView主要是用来显示图片的控件,可以对图片进行发大、缩小和旋转的功能。android:scaleType属性指定ImageView控件显示图片的方式,例如:Center表示图像以不缩放的方式显示在ImageView控件的中心,如果设置为fitCenter,表示图像按照比例缩放至合适的位置,并在ImageView控件的中心。

    下面介绍ImageView的基本用法。

    一、建立工程,如图

    二、activity_main.xml中代码

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TextView android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:text="scaleType:center 为缩放,放在ImageView的中心" 
            />
        <ImageView android:id="@+id/imageview" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:background="#F00"
            android:src="@drawable/background" 
            android:scaleType="center">
        </ImageView>
    
        <TextView android:layout_width="fill_parent"
            android:layout_marginTop="20dp" 
            android:layout_height="wrap_content"
            android:text="scaleType:fitCenter 按照比例缩放" />
    
        <ImageView android:id="@+id/imageview2" 
            android:layout_width="300dp"
            android:layout_height="200dp" 
            android:background="#FFF"
            android:src="@drawable/background" 
            android:scaleType="fitCenter"
            android:padding="10dp">
        </ImageView>
    </LinearLayout>
    View Code

    三、Mainactivity.java中代码

    package com.study.imageview1;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    
    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            ImageView imageView = (ImageView)this.findViewById(R.id.imageview);
            //设置第一个图片的比例大小
            //表示宽度:200,高度:100
            imageView.setLayoutParams(new LinearLayout.LayoutParams(200, 100));
            setTitle("height:" + imageView.getLayoutParams().height +"--width-->" + imageView.getLayoutParams().width);
            
        }
    
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
        
    }
    View Code

    四、效果图

    从上图中,可以看到,图片是被裁减了一定区域,显示不完全。而下面的图片是按照一定比例缩放显示的,主要由属性:android:scaleType="fitCenter" 控制的。

    background.jpg图片:

  • 相关阅读:
    mysql 实战 or、in与union all 的查询效率
    转:手机流畅的决定性因素
    合批只是对CPU的优化,与GPU没有任何关系
    UNITY 打包时提示sdk tools 或 sdk build tools版本低时可以直接点update 按钮进行更新
    RGB ECT 4BIT 压缩后质量远高于RGB ETC2 4BIT
    Adreno GPU Profiler
    UNITY2018.3 在editor下运行时new memoryprofiler显示 shader占用内存很大的问题在安卓上并没有看到
    VS2017断点调试UNITY2018.3 经常卡住的问题
    一次UNITY闪退问题的定位心得
    UNITY2018 真机开启deepprofiling的操作
  • 原文地址:https://www.cnblogs.com/kingshow123/p/imageview.html
Copyright © 2011-2022 走看看