ImageView简单实例

1.界面部分

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/next"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="下一张" />

        <Button
            android:id="@+id/plus"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="加" />

        <Button
            android:id="@+id/mins"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="减" />
    </LinearLayout>

    <ImageView
        android:id="@+id/image1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/i1" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/image2"
            android:layout_width="108dp"
            android:layout_height="72dp"
            android:src="@drawable/i2" />

        <QuickContactBadge
           	android:id="@+id/contact"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:src="@drawable/i5" />

    </LinearLayout>

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/drawable_selector" />

</LinearLayout>
2.代码部分:

package com.example.imageviewtest;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Menu;
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;
import android.widget.QuickContactBadge;

public class MainActivity extends Activity {

	Button next, plus, mins;
	ImageView image1, image2;
	QuickContactBadge badge;//关联联系人
	int[] image = new int[] { R.drawable.i1, R.drawable.i2, R.drawable.i3,
			R.drawable.i4, R.drawable.i5 };
	private int currentImage=1;//默认图片
	private int alpha=255;//默认透明度
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
      
        setContentView(R.layout.activity_main);
        next=(Button) findViewById(R.id.next);//下一张
        plus=(Button) findViewById(R.id.plus);//增加透明度
        mins=(Button) findViewById(R.id.mins);//减少透明度
        image1=(ImageView) findViewById(R.id.image1);
        image2=(ImageView) findViewById(R.id.image2);
        badge=(QuickContactBadge) findViewById(R.id.contact);
        badge.assignContactFromPhone("12345", false);//关联联系人电话号码
        OnClickListener Listener = new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				switch(v.getId()){
					case R.id.plus:
						if(alpha>255){
							alpha=255;
						}else{
							alpha+=20;
						}
						image1.setAlpha(alpha);
						break;
					case R.id.mins:
						if(alpha<0){
							alpha=0;
						}else{
							alpha-=20;
						}
						image1.setAlpha(alpha);
						break;				
				}
			}
		};
		plus.setOnClickListener(Listener);
		mins.setOnClickListener(Listener);
        next.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				image1.setImageResource(image[++currentImage%image.length]);//下一张循环
			}
		});
        
        image1.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View arg0, MotionEvent event) {
				// TODO Auto-generated method stub
				BitmapDrawable bitmapdrawable=(BitmapDrawable) image1.getDrawable();//获得image1的图片
				Bitmap bitmap=bitmapdrawable.getBitmap();//变成位图
				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(120);//设置透明度
				return false;
			}
		});
       
    }

	@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;
	}

}
3.其他

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_focused="true" android:drawable="@drawable/i1"></item>
<item android:state_enabled="false" android:drawable="@drawable/i2"></item>  
<item android:state_pressed="true" android:drawable="@drawable/i3"></item>
<item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/i4"></item>
<item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/i5"></item>
</selector>


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值