ImageView透明度

ImageView 能显示的不仅仅是图片,任何Drawable对象都可以使用它来显示

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">
<LinearLayout
	android:orientation="horizontal" 
	android:layout_width="fill_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 -->
<ImageView android:id="@+id/image1"
	android:layout_width="fill_parent"
	android:background="#0000ff" 
	android:layout_height="240px"
	android:src="@drawable/shuangta"
	android:scaleType="fitCenter"  />
<!-- 定义显示图片局部细节的ImageView -->	
<ImageView android:id="@+id/image2" 
	android:layout_width="120dp"
	android:layout_height="120dp"
	android:background="#0000ff" 	
	android:layout_marginTop="10dp"/>
</LinearLayout>

Activity

package org.crazyit.imageview;

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;

/**
 * Description:
 * <br/>site: <a href="http://www.crazyit.org">crazyit.org</a> 
 * <br/>Copyright (C), 2001-2012, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee kongyeeku@163.com
 * @version  1.0
 */
public class ImageViewTest extends Activity
{
	//定义一个访问图片的数组
	int[] images = new int[]{
		R.drawable.lijiang,
		R.drawable.qiao,
		R.drawable.shuangta,
		R.drawable.shui,
		R.drawable.xiangbi,
	};
	//定义默认显示的图片
	int currentImg = 2;
	//定义图片的初始透明度
	private int alpha = 255;
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		final Button plus = (Button)findViewById(R.id.plus);
		final Button minus = (Button)findViewById(R.id.minus);
		final ImageView image1 = (ImageView)findViewById(R.id.image1);
		final ImageView image2 = (ImageView)findViewById(R.id.image2);
		final Button next = (Button)findViewById(R.id.next);
		//定义查看下一张图片的监听器
		next.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				if (currentImg >= 4)
				{
					currentImg = -1;
				}
				BitmapDrawable bitmapDrawable = (BitmapDrawable) image1
					.getDrawable();
				//如果图片还未回收,先强制回收该图片
				if (!bitmapDrawable.getBitmap().isRecycled())
				{
					bitmapDrawable.getBitmap().recycle();
				}
				//改变ImageView显示的图片
				image1.setImageBitmap(BitmapFactory.decodeResource(getResources()
					, images[++currentImg]));
//				try
//				{
//					//改变ImageView里显示的图片
//					image1.setImageResource(images[++currentImg]);
//				}
//				//捕捉内存溢出异常
//				catch(OutOfMemoryError e)
//				{
//				}
			}
		});
		//定义改变图片透明度的方法
		OnClickListener listener = new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				if(v == plus)
				{
					alpha += 20;
				}
				if(v == minus)
				{
					alpha -= 20;
				}
				if(alpha >= 255)
				{
					alpha = 255;
				}
				if(alpha <= 0)
				{
					alpha = 0;
				}
				//改变图片的透明度
				image1.setAlpha(alpha);				
			}
		};
		//为两个按钮添加监听器
		plus.setOnClickListener(listener);
		minus.setOnClickListener(listener);
		image1.setOnTouchListener(new OnTouchListener()
		{
			@Override
			public boolean onTouch(View view, MotionEvent event)
			{
				BitmapDrawable bitmapDrawable = (BitmapDrawable) image1
					.getDrawable();
				//获取第一个图片显示框中的位图
				Bitmap bitmap = bitmapDrawable.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())
				{
					y = bitmap.getHeight() - 120;
				}
				//显示图片的指定区域
				image2.setImageBitmap(Bitmap.createBitmap(bitmap, x, y, 120, 120));
				image2.setAlpha(alpha);
				return false;
			}
		});
	}
}


转载于:https://my.oschina.net/mutouzhang/blog/207036

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Android开发中,通过改变ImageView透明度实现渐变效果可以通过以下步骤实现: 步骤1:在布局文件中添加一个ImageView控件。 步骤2:在代码中找到ImageView控件,并使用属性动画来改变其透明度。 首先,创建一个AlphaAnimation对象,并设置起始透明度为0(完全透明)和结束透明度为1(完全不透明)。 然后,创建一个AnimatorSet对象,并将AlphaAnimation添加为其子动画。 最后,使用AnimatorSet的start方法来启动动画。 步骤3:运行程序,你将看到ImageView透明度渐变的效果。 以下是一个简单的示例代码: XML布局文件: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/image" /> </LinearLayout> ``` Java代码: ```java import android.animation.ObjectAnimator; import android.animation.ValueAnimator; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ImageView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ImageView imageView = findViewById(R.id.imageView); ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(imageView, "alpha", 0f, 1f); alphaAnimator.setDuration(2000); alphaAnimator.setRepeatCount(ValueAnimator.INFINITE); alphaAnimator.setRepeatMode(ValueAnimator.REVERSE); alphaAnimator.start(); } } ``` 在这个示例中,我们让imageView透明度在0~1之间进行渐变,渐变的持续时间是2秒,并且设置了无限重复和来回循环的模式。 这样,通过改变ImageView透明度,你可以实现一个渐变的效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值