ImageView中的一些用法

这个demo实现了点击按钮 改变图片的透明度,对图片的局部放大。有一点bitmap位图的内容有些不太明白。后期会专门研究这个类。

MainActivity.java

package lzl.edu.com.imageviewtest;

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

public class FirstActivity extends Activity implements OnClickListener{

    Button increase,reduce,next;
    ImageView image1,image2;
    private int alpha=255;
    int images[] = {R.mipmap.my_hotel_a,R.mipmap.my_hotel_b,R.mipmap.my_hotel_c,
            R.mipmap.my_hotel_d,R.mipmap.my_hotel_e};
    int currentImage = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }
    void init(){
        increase = (Button)findViewById(R.id.increase);
        reduce = (Button)findViewById(R.id.reduce);
        next = (Button)findViewById(R.id.next);
        image1 = (ImageView)findViewById(R.id.image1);
        image2 = (ImageView)findViewById(R.id.image2);
        increase.setOnClickListener(this);
        reduce.setOnClickListener(this);
        next.setOnClickListener(this);

        image1.setOnTouchListener(new touchImages());
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.increase){
            alpha +=5;
            Log.i("增加",""+alpha);
        }
        if(v.getId() == R.id.reduce){
            alpha -= 5;
            Log.i("减少",""+alpha);
        }
        if(v.getId()==R.id.next){
            nextImageSource();
        }
        if(alpha >255){
            alpha = 255;
        }
        if(alpha <0){
            alpha = 0;
        }
        //改变图片的透明度
        image2.setAlpha(alpha);
    }
    //查看下一张图片
    public void nextImageSource(){
        if(currentImage >= images.length){
            currentImage = -1;
        }
        BitmapDrawable bitmapDrawable = (BitmapDrawable)image1.getDrawable();
                //如果图片还未回收,先强制回收该图片
        if(!bitmapDrawable.getBitmap().isRecycled()){
            bitmapDrawable.getBitmap().recycle();
        }
        //改变ImageView显示的图片
        image1.setImageBitmap(BitmapFactory.decodeResource(getResources(),images[currentImage]));
        currentImage++;
    }
    //接触到图片的位置,在下方放大

    class touchImages implements View.OnTouchListener{
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            Log.i("viewid----",""+v.getId());
            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;
        }
    }
}
activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
    >
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/increase"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="20dp"
            android:text="增加"/>
        <Button
            android:id="@+id/reduce"
            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>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    <ImageView
        android:id="@+id/image1"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:src="@mipmap/my_hotel"
        android:scaleType="fitCenter"
        />
        <ImageView
        android:id="@+id/image2"
        android:layout_width="300dp"
        android:layout_height="300dp"
            android:src="@mipmap/my_hotel"
        android:contentDescription="124" />
    </LinearLayout>
    </LinearLayout>
    </RelativeLayout>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值