android ImageView制作图片

ImageView继承自View组件,主要功能是用来显示图片的。

其中一写常见属性:

创建一个图片浏览器

<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
 
    <LinearLayout 
       	 android:id="@+id/linearbutton"
         android:orientation="horizontal"
   		 android:layout_width="match_parent"
   		 android:layout_height="wrap_content"
   		 android:gravity="center"
        >
        <Button
        android:id="@+id/plus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="增大透明度" />
        
        <Button
        android:id="@+id/minus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="减少透明度" />
         
        <Button
        android:id="@+id/next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="下一张" />
    </LinearLayout>
    <ImageView 
        android:id="@+id/image1"
        android:layout_width="match_parent"
        android:background="#0000ff"
        android:layout_height="240px"
        android:src="@drawable/pp1"
        android:scaleType="fitCenter"
        />
    <ImageView 
        android:id="@+id/image2"
        android:layout_width="120dp"
        android:background="#0000ff"
        android:layout_height="120dp"
        android:layout_marginTop="10dp"
        />
</LinearLayout>
Java代码

package com.example.demo;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;

public class picture extends Activity {
	

	private int[] images = new int[]{
		R.drawable.pp1,
		R.drawable.pp2,
		R.drawable.pp3,
		R.drawable.pp4,
		R.drawable.pp5,
		R.drawable.pp6,
	};
	
	//定义默认显示的图片
	private int muns = 0;
	//定义图片的透明度
	private int alpha = 255;
	
	private Button button1;
	private Button button2;
	private Button button3;
	
	private ImageView image1;
	private ImageView image2;
	
	@Override
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.picture);
		
		button1 = (Button) findViewById(R.id.plus);
		button2 = (Button) findViewById(R.id.minus);
		button3 = (Button) findViewById(R.id.next);
		image1 = (ImageView) findViewById(R.id.image1);
		image2 = (ImageView) findViewById(R.id.image2);
		
		button3.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(muns>=5){
					muns = 0;
				}
				muns++;
				BitmapDrawable bit = (BitmapDrawable) image1.getDrawable();
				//如果图片没有回收,先强制回收该图片
				if(bit.getBitmap().isRecycled()){
					bit.getBitmap().recycle();
				}
				//改变ImageView显示的图片
				image1.setImageBitmap(BitmapFactory.decodeResource(getResources(),images[muns]));
			}
			
		});
		
		button3.setOnTouchListener(new OnTouchListener(){

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				BitmapDrawable bit = (BitmapDrawable) image1.getDrawable();
				//获取第一个图片显示框中的位图
				Bitmap bitmap = bit.getBitmap();
				//bitmap图片实际大小与第一个ImageView的缩放比例
				double scale = bitmap.getWidth() /320.0;
				//获取需要显示的图片的开始点
				int x = (int)(event.getX()*scale);
				int y = (int)(event.getY()*scale);
				if(x + 120>bitmap.getWidth()){
					x = bitmap.getWidth() - 120;
				}
				if(y + 120>bitmap.getHeight()){
					x = bitmap.getHeight() - 120;
				}
				//显示图片的指定区域
				image2.setImageBitmap(Bitmap.createBitmap(bitmap, x, y, 120, 120));
				image2.setAlpha(alpha);
				return false;
			}});
		
		OnClickListener listener = new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(v == button1){
					alpha += 20;
				}
				if(v == button2){
					alpha -= 20;
				}
				if(alpha>=255){
					alpha = 255;
				}
				if(alpha<=0){
					alpha = 0;
				}
				//改变图片透明度
				image1.setAlpha(alpha);
			}
			
		};
		//必须先定义listener
		button1.setOnClickListener(listener);
		button2.setOnClickListener(listener);
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值