疯狂Android之图片浏览器

来源:互联网 发布:ipad可以开淘宝店吗 编辑:程序博客网 时间:2024/05/01 17:59
package com.sunset.imageviewfirst;


import android.app.Activity;
import android.graphics.Bitmap;
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 MainActivity extends Activity {


int[] images = new int[] { R.drawable.icon_two, R.drawable.fdsfdsafdsaf,
R.drawable.icon_odfde, R.drawable.icon_ofdfd };


int count = 2;
private int alpha = 255;
Button but_1, but_2, but_3;
ImageView imageView1, imageView2;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
but_3 = (Button) findViewById(R.id.but_3);
but_1 = (Button) findViewById(R.id.but_1);
but_2 = (Button) findViewById(R.id.but_2);
imageView1 = (ImageView) findViewById(R.id.imageView1);
imageView2 = (ImageView) findViewById(R.id.imageView2);
but_3.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
imageView1.setImageResource(images[++count % images.length]);
}
});


View.OnClickListener listtener = new View.OnClickListener() {


@Override
public void onClick(View v) {
if (v == but_1) {
alpha += 20;
}
if (v == but_2) {
alpha -= 20;
}
if (alpha >= 255) {
alpha += 255;
}
if (alpha <= 0) {
alpha += 0;
}
imageView1.setAlpha(alpha);
}
};
but_1.setOnClickListener(listtener);
but_2.setOnClickListener(listtener);


imageView1.setOnTouchListener(new OnTouchListener() {


@Override
public boolean onTouch(View v, MotionEvent event) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView1
.getDrawable();
Bitmap bitmap = bitmapDrawable.getBitmap();


double scale = 1.0 * bitmap.getHeight() / bitmap.getWidth();
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;
}
imageView2.setImageBitmap(Bitmap.createBitmap(bitmap, x, y,
120, 120));
return false;
}
});
}

}






xml文件activity_main


<LinearLayout 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:orientation="vertical">


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


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


        <Button
            android:id="@+id/but_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="-" />


        <Button
            android:id="@+id/but_3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="下一張" />
    </LinearLayout>


    <ImageView
          android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="280dp"
        android:scaleType="fitCenter"
        android:src="@drawable/icon_two" />


    <ImageView
          android:id="@+id/imageView2"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:background="#00f" />


</LinearLayout>

1 0