實現探照燈效果

never123450發表於2014-07-11

activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/frameLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.xwy.TanZhaoDeng.MainActivity"
    tools:ignore="MergeRootFrame" />


MainActivity.java

package com.xwy.TanZhaoDeng;
import android.support.v4.app.Fragment;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		FrameLayout l1 = (FrameLayout) findViewById(R.id.frameLayout1);
		l1.addView(new MyView(this));// 將自定義檢視新增到幀佈局管理器中
	}
	
	public class MyView extends View{
		private Bitmap bitmap;// 源影象,也就是背景影象
		private ShapeDrawable drawable;
		private final int RADIUS = 100;// 探照燈的半徑
		private Matrix matrix = new Matrix();
		
		public MyView(Context context){
			super(context);
			Bitmap bitmap_source = BitmapFactory.decodeResource(getResources(), R.drawable.source);//獲取要顯示的源影象
			bitmap = bitmap_source;
			BitmapShader shader = new BitmapShader(Bitmap.createScaledBitmap(
					bitmap_source, bitmap_source.getWidth(),
					bitmap_source.getHeight(), true), TileMode.CLAMP,
					TileMode.CLAMP);//建立BitmapShader物件
			
			//圓形的drawable
			drawable = new ShapeDrawable(new OvalShape());
			drawable.getPaint().setShader(shader);
			drawable.setBounds(0,0,RADIUS*2,RADIUS*2);// 設定圓的外切矩形
		}
		protected void onDraw(Canvas canvas){
			super.onDraw(canvas);
			Paint p = new Paint();
			p.setAlpha(50);
			canvas.drawBitmap(bitmap, 0, 0,p);// 繪製背景影象
			drawable.draw(canvas);// 繪製探照燈照射的影象
		}
		
		public boolean onTouchEvent(MotionEvent event){
			final int x = (int) event.getX();// 獲取當前觸控點的X軸座標
			final int y = (int) event.getY(); // 獲取當前觸控點的Y軸座標
			matrix.setTranslate(RADIUS - x, RADIUS - y);// 平移到繪製shader的起始位置
			drawable.getPaint().getShader().setLocalMatrix(matrix);
			drawable.setBounds(x-RADIUS,y-RADIUS,x+RADIUS,y+RADIUS);// 設定圓的外切矩形
			invalidate(); // 重繪畫布
			return true;
		}
	}
	

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

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_main, container,
					false);
			return rootView;
		}
	}

}


相關文章