• ANDROID笔记:ImageView的简单使用


     1 package com.example.classtest;
     2 
     3 import android.app.Activity;
     4 import android.graphics.Bitmap;
     5 import android.graphics.BitmapFactory;
     6 import android.graphics.Matrix;
     7 import android.os.Bundle;
     8 import android.view.View;
     9 import android.view.View.OnClickListener;
    10 import android.widget.Button;
    11 import android.widget.ImageView;
    12 import android.widget.ZoomButton;
    13 
    14 public class ImageViewActivity extends Activity implements OnClickListener {
    15     // 透明度取值:0-255
    16     int alpha = 255;
    17     ImageView imageView = null;
    18     float towidth = 1;
    19     float toheight = 1;
    20     Bitmap bitmapsrc = null;
    21     int bwidth;
    22     int bheight;
    23 
    24     @Override
    25     protected void onCreate(Bundle savedInstanceState) {
    26         super.onCreate(savedInstanceState);
    27         setContentView(R.layout.imageview);
    28         // 缩放按钮
    29         ZoomButton button3 = (ZoomButton) findViewById(R.id.btn3);
    30         ZoomButton button4 = (ZoomButton) findViewById(R.id.btn4);
    31         button3.setOnClickListener(this);
    32         button4.setOnClickListener(this);
    33         // 设置透明度的按钮
    34         Button button1 = (Button) findViewById(R.id.btn1);
    35         Button button2 = (Button) findViewById(R.id.btn2);
    36         button1.setOnClickListener(this);
    37         button2.setOnClickListener(this);
    38         imageView = (ImageView) findViewById(R.id.image);
    39         // 得到需要缩放的图片资源
    40         bitmapsrc = BitmapFactory.decodeResource(getResources(),
    41                 R.drawable.ic_launcher);
    42         bwidth = bitmapsrc.getWidth();
    43         bheight = bitmapsrc.getHeight();
    44 
    45     }
    46 
    47     @Override
    48     public void onClick(View v) {
    49         Matrix matrix = new Matrix();
    50         Bitmap bitmap = null;
    51         switch (v.getId()) {
    52         case R.id.btn3:
    53             // 放大图片
    54             towidth = (float) (towidth * 1.25);
    55             toheight = (float) (toheight * 1.25);
    56             // 矩阵计算
    57             matrix.postScale(towidth, toheight);
    58             bitmap = Bitmap.createBitmap(bitmapsrc, 0, 0, bwidth, bheight,
    59                     matrix, true);
    60             imageView.setImageBitmap(bitmap);
    61             break;
    62         case R.id.btn4:
    63             towidth = (float) (towidth * 0.75);
    64             toheight = (float) (toheight * 0.75);
    65 
    66             matrix.postScale(towidth, toheight);
    67             bitmap = Bitmap.createBitmap(bitmapsrc, 0, 0, bwidth, bheight,
    68                     matrix, true);
    69             imageView.setImageBitmap(bitmap);
    70             break;
    71         case R.id.btn1:
    72             alpha += 40;
    73             if (alpha > 255) {
    74                 alpha = 255;
    75             }
    76             // 设置图片透明度
    77             imageView.setAlpha(alpha);
    78             break;
    79         case R.id.btn2:
    80             alpha -= 40;
    81             if (alpha < 0) {
    82                 alpha = 0;
    83             }
    84             imageView.setAlpha(alpha);
    85             break;
    86 
    87         }
    88 
    89     }
    90 }
     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:orientation="vertical" >
     6 
     7     <!-- 横向滚动条 -->
     8 
     9     <HorizontalScrollView
    10         android:layout_width="320px"
    11         android:layout_height="wrap_content" >
    12 
    13         <!-- 纵向滚动条 -->
    14 
    15         <ScrollView
    16             android:layout_width="wrap_content"
    17             android:layout_height="200px" >
    18 
    19             <ImageView
    20                 android:id="@+id/image"
    21                 android:layout_width="wrap_content"
    22                 android:layout_height="350dp"
    23                 android:scaleType="centerInside"
    24                 android:src="@drawable/ic_launcher" />
    25         </ScrollView>
    26     </HorizontalScrollView>
    27 
    28     <LinearLayout
    29         android:layout_width="fill_parent"
    30         android:layout_height="wrap_content"
    31         android:orientation="horizontal" >
    32 
    33         <Button
    34             android:id="@+id/btn1"
    35             android:layout_width="wrap_content"
    36             android:layout_height="wrap_content"
    37             android:text="增加透明度" />
    38 
    39         <Button
    40             android:id="@+id/btn2"
    41             android:layout_width="wrap_content"
    42             android:layout_height="wrap_content"
    43             android:text="减小透明度" />
    44 
    45     </LinearLayout>
    46 
    47     <LinearLayout
    48         android:layout_width="wrap_content"
    49         android:layout_height="wrap_content"
    50         android:orientation="horizontal" >
    51 
    52         <!-- 缩放按钮 -->
    53 
    54         <ZoomButton
    55             android:id="@+id/btn3"
    56             android:layout_width="wrap_content"
    57             android:layout_height="wrap_content"
    58             android:src="@android:drawable/btn_plus" />
    59 
    60         <ZoomButton
    61             android:id="@+id/btn4"
    62             android:layout_width="wrap_content"
    63             android:layout_height="wrap_content"
    64             android:src="@android:drawable/btn_minus" />
    65     </LinearLayout>
    66 
    67 </LinearLayout>

    重点:

      图片的透明度:imageView.setAlpha(alpha);(alpha 0-255)

      图片的缩放:    matrix.postScale(towidth, toheight);
                     bitmap = Bitmap.createBitmap(bitmapsrc, 0, 0, bwidth, bheight, matrix, true);

             imageView.setImageBitmap(bitmap);

  • 相关阅读:
    bean的singleton(没有看到生命周期范围??)
    ApplicationContext中getBean详解
    mybatis基础之二
    mybatis基础之一
    JDOM读取xml
    SSH整合最简单的一个例子
    基础学习问题
    2015年2.9--2.15号第一周计划
    freertos的钩子函数
    IAR环境搭建注意点
  • 原文地址:https://www.cnblogs.com/afluy/p/3370498.html
Copyright © 2020-2023  润新知