Android开发经验之在图片上随意点击移动文字

 

 

只要在图片范围之内,文字可随意点击移动。

package xiaosi.GetTextImage;


import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;

public class GetTextImage extends View
{
	private float x = 20, y = 40;
	private static float windowWidth;
	private static float windowHeight;
	private static float left = 0;      //图片在屏幕中位置X坐标
	private static float top = 0;       //图片在屏幕中位置Y坐标
	private String str = "我爱你";
	private DisplayMetrics dm = new DisplayMetrics();  //用于获取屏幕的高度和宽度
	private WindowManager windowManager;
	private Bitmap newbitmap;

	public GetTextImage(Context context)
	{
		super(context);
		windowManager = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		//屏幕的宽度
		windowWidth = windowManager.getDefaultDisplay().getWidth();
		//屏幕的高度
		windowHeight = windowManager.getDefaultDisplay().getHeight();
	}

	public void onDraw(Canvas canvas)
	{
		Resources res = getResources();
		Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.b);
		newbitmap = getTextImage(bmp, str, x, y);
		canvas.drawBitmap(newbitmap, 0, 0, null);
	}
	/**
	 * 返回值: Bitmap 参数:原图片,文字 功能: 根据给定的文字生成相应图片
	 * 
	 * @param originalMap 
	 * @param text  文字
	 * @param x  点击的X坐标
	 * @param y  点击的Y坐标
	 * @return
	 */
	public static Bitmap getTextImage(Bitmap originalMap, String text, float x,
			float y)
	{
		float bitmapWidth = originalMap.getWidth();
		float bitmapHeight = originalMap.getHeight();
		// 定义画布
	    Canvas canvas = new Canvas(originalMap);
	    // 定义画笔
		Paint paint = new Paint();
		//获得文本的长度(像素)
		float textWidth = paint.measureText(text); 
		canvas.drawBitmap(originalMap, 0, 0, null);
		
		// 如果图片宽度小于屏幕宽度
		if (left + bitmapWidth < windowWidth)
		{
			// 右边界
			if (x >= left + bitmapWidth - textWidth)
			{
				x = left + bitmapWidth - textWidth;
			}
			// 左边界
			else if (x <= left)
			{
				x = left;
			}
		}
		else
		{
			// 右边界
			if (x >= windowWidth - textWidth)
			{
				x = windowWidth - textWidth;
			}
			// 左边界
			else if (x <= 0)
			{
				x = 0;
			}
		}
		// 如果图片高度小于屏幕高度
		if (top + bitmapHeight < windowHeight)
		{
			// 下
			if (y >= top + bitmapHeight)
			{
				y = top + bitmapHeight;
			}
			// 上
			else if (y <= top + 10)
			{
				y = top + 10;
			}
		}
		else
		{
			if (y >= windowHeight)
			{
				y = windowHeight;
			}
			else if (y <= 0)
			{
				y = 0;
			}
		}
		
		// 添加字
	    canvas.drawText(text, x, y, paint);
		return originalMap;
	}
	@Override
	public boolean onTouchEvent(MotionEvent event)
	{
		if (event.getAction() == MotionEvent.ACTION_DOWN)
		{
			x = event.getX();
			y = event.getY();
			// 重绘
			invalidate();
		}
		return true;
	}
}


package xiaosi.GetTextImage;

import android.app.Activity;
import android.os.Bundle;

public class GetTextImageActivity extends Activity {
    /** Called when the activity is first created. */
	private GetTextImage get;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        get = new GetTextImage(this);
        setContentView(get);
    }
}


 

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值