实现探照灯效果

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

}


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值