<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/layoutMain" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center"> <Button android:id="@+id/upAlpha" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="增大透明度" /> <Button android:id="@+id/downAlpha" 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="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > </LinearLayout> <ImageView android:id="@+id/imageView1" android:layout_width="match_parent" android:layout_height="400px" android:background="#0000ff" android:scaleType="fitCenter" android:src="@drawable/camera" /> <ImageView android:id="@+id/imageView2" android:layout_width="120dp" android:layout_height="120dp" android:background="#0000ff" android:layout_marginTop="10dp" android:src="@drawable/camera" /> </LinearLayout>
package com.moon.hello2; import java.util.Date; import java.util.Timer; import java.util.TimerTask; import com.moon.hello2.R.layout; import android.R.integer; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.os.SystemClock; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.drawable.BitmapDrawable; import android.view.Menu; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnTouchListener; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; import android.widget.Button; import android.widget.Chronometer; import android.widget.Chronometer.OnChronometerTickListener; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.ToggleButton; public class MainActivity extends Activity { int[] images = new int[] { R.drawable.a1, R.drawable.a2, R.drawable.a3, R.drawable.a4, R.drawable.a5 }; int currentImg = 0; int alpha = 255; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.activity_main); final ImageView iView = (ImageView) findViewById(R.id.imageView1); final ImageView iView2 = (ImageView) findViewById(R.id.imageView2); final Button btnUpAlpha = (Button) findViewById(R.id.upAlpha); final Button btnDownAlpha = (Button) findViewById(R.id.downAlpha); Button btnNext = (Button) findViewById(R.id.next); iView.setImageResource(images[this.currentImg]); btnNext.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub currentImg++; if (currentImg >= images.length) { currentImg = 0; } BitmapDrawable bitmapDrawable = (BitmapDrawable) iView .getDrawable(); if (!bitmapDrawable.getBitmap().isRecycled()) { bitmapDrawable.getBitmap().recycle(); } iView.setImageBitmap(BitmapFactory.decodeResource( getResources(), images[currentImg])); } }); OnClickListener listener = new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if (v == btnUpAlpha) { alpha += 20; } else if (v == btnDownAlpha) { alpha -= 20; } else { ; } if (alpha > 255) { alpha = 255; } else if (alpha < 0) { alpha = 0; } else { ; } iView.setAlpha(alpha); } }; btnDownAlpha.setOnClickListener(listener); btnUpAlpha.setOnClickListener(listener); iView.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View arg0, MotionEvent event) { // TODO Auto-generated method stub BitmapDrawable bitmapDrawable = (BitmapDrawable) iView .getDrawable(); 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; } iView2.setImageBitmap(Bitmap.createBitmap(bitmap,x,y,120,120)); iView2.setAlpha(alpha); return false; } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } }