Android 自定义View 分区域监听 回调

借助于这位博主的文章:大牛关于自定义view的一系列的文章,还有宏洋这位大牛的博客:http://blog.csdn.net/lmj623565791/article/details/24252901/.

详细的解释可以看这两位博主的博客,写的非常好,我就不多此一举了.我顺着写写怎么根据点击的xy坐标,来对部分区域进行点击的监听操作,详细的请看代码:

package com.example.customview01.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

import com.example.customview01.R;
import com.example.customview01.event.IStationEventName;
import com.example.customview01.event.StationEventCaptain;
import com.example.customview01.event.StationEventData;

/**
 * 图形左上角的买卖自定义View,需要对部分区域进行监听.点击商品弹出选择商品的界面
 * 
 * @author qiulinhe
 * @createTime 2016年7月1日 上午11:47:00
 */
public class CustomTitleView extends View {
	/**
	 * 文本
	 */
	private String mTitleText;
	/**
	 * 文本的颜色
	 */
	private int mTitleTextColor;
	/**
	 * 文本的大小
	 */
	private int mTitleSellBuySize;

	private int mTitleTextSize;

	/**
	 * 绘制时控制文本绘制的范围
	 */
	private Rect mBound;
	private Rect mBoundSellBuy;
	//设置商品的字体和大小
	private Paint instrumentPaint;

	//设置买入和卖出的颜色和字体大小
	private Paint mPaintSellBuy;
	private Paint mPaintCeshi;

	//买入和卖出的背景图
	private Bitmap sellBitmapBackGround;
	private Bitmap buyBitmapBackGround;

	private Context context;

	public CustomTitleView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public CustomTitleView(Context context) {
		this(context, null);
	}

	/**
	 * 获得我自定义的样式属性,从attr.xml里面读取
	 * 
	 * @param context
	 * @param attrs
	 * @param defStyle
	 */
	public CustomTitleView(final Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.context = context;
		/**
		 * 获得我们在attr.xml文件里面设置的文字颜色和尺寸大小信息.所定义的自定义样式属性
		 */
		TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomTitleView, defStyle, 0);
		int n = a.getIndexCount();
		for (int i = 0; i < n; i++) {
			int attr = a.getIndex(i);
			switch (attr) {
			case R.styleable.CustomTitleView_titleText:
				mTitleText = a.getString(attr);
				break;
			case R.styleable.CustomTitleView_titleTextColor:
				// 默认颜色设置为黑色
				mTitleTextColor = a.getColor(attr, Color.BLACK);
				break;
			case R.styleable.CustomTitleView_titleTextSize:
				// 默认设置为16sp,TypeValue也可以把sp转化为px
				mTitleTextSize = a.getDimensionPixelSize(attr,
						(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));
				mTitleSellBuySize = a.getDimensionPixelSize(attr,
						(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 22, getResources().getDisplayMetrics()));

				break;

			}

		}
		a.recycle();

		//设置商品显示部分的宽高和字体大小颜色
		instrumentPaint = new Paint();
		instrumentPaint.setTextSize(mTitleTextSize);
		// mPaint.setColor(mTitleTextColor);
		mBound = new Rect();
		mBound.set(0, 0, 500, 500);
		instrumentPaint.getTextBounds(mTitleText, 0, mTitleText.length(), mBound);

		//设置买入和卖出显示部分的宽高和字体大小颜色
		mPaintSellBuy = new Paint();
		mPaintSellBuy.setTextSize(mTitleSellBuySize);
		mBoundSellBuy = new Rect();
		mBoundSellBuy.set(0, 0, 800, 800);
		mPaintSellBuy.getTextBounds(mTitleText, 0, mTitleText.length(), mBoundSellBuy);

		//对商品和买入和卖出进行监听
		//		this.setOnClickListener(new OnClickListener() {
		//
		//			@Override
		//			public void onClick(View v) {
		//				Toast.makeText(context, "您点击了这个界面,恭喜你!!!", Toast.LENGTH_LONG).show();
		//				postInvalidate();
		//			}
		//
		//		});

		//对背景图片进行初始化,从资源传进来.
		init(context);

	}

	/**
	 * 对买入和卖出的背景资源进行加载
	 * 
	 * @param context
	 * @author qiulinhe
	 * @createTime 2016年7月1日 上午11:50:40
	 */
	private void init(Context context) {
		mPaintCeshi = new Paint();
		mPaintCeshi.setAntiAlias(true);

		sellBitmapBackGround = BitmapFactory.decodeResource(getResources(), R.drawable.pair_sell_select);
		buyBitmapBackGround = BitmapFactory.decodeResource(getResources(), R.drawable.ordertrade_pair_buy_select);

		//bitmapBackGround = Bitmap.createBitmap(500,200, Bitmap.Config.ARGB_8888);

	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// super.onMeasure(widthMeasureSpec, heightMeasureSpec);

		int width = 0;
		int height = 0;

		/**
		 * 设置宽度
		 */
		int specMode = MeasureSpec.getMode(widthMeasureSpec);
		int specSize = MeasureSpec.getSize(widthMeasureSpec);
		switch (specMode) {
		case MeasureSpec.EXACTLY:// 明确指定了
			width = getPaddingLeft() + getPaddingRight() + specSize;
			break;
		case MeasureSpec.AT_MOST:// 一般为WARP_CONTENT
			width = getPaddingLeft() + getPaddingRight() + mBound.width();
			break;
		}

		/**
		 * 设置高度
		 */
		specMode = MeasureSpec.getMode(heightMeasureSpec);
		specSize = MeasureSpec.getSize(heightMeasureSpec);
		switch (specMode) {
		case MeasureSpec.EXACTLY:// 明确指定了
			height = getPaddingTop() + getPaddingBottom() + specSize;
			break;
		case MeasureSpec.AT_MOST:// 一般为WARP_CONTENT
			height = getPaddingTop() + getPaddingBottom() + mBound.height();
			break;
		}

		setMeasuredDimension(width, height);

	}

	@Override
	protected void onDraw(Canvas canvas) {
		//设置买入卖出的背景图片
		canvas.drawBitmap(sellBitmapBackGround, 0, 0, instrumentPaint);
		canvas.drawBitmap(buyBitmapBackGround, 297, 0, instrumentPaint);

		//设置商品部分的背景和字体颜色
		instrumentPaint.setColor(Color.GRAY);
		canvas.drawRect(210, 0, 360, 50, instrumentPaint);
		instrumentPaint.setColor(mTitleTextColor);
		canvas.drawText("AUD/JPY", getWidth() / 2 - (mBound.width() + 29), getHeight() / 4 + mBound.height() / 4 - 17,
				instrumentPaint);

		//设置买入卖出部分的字体和颜色
		//		SpannableString spannableString = new SpannableString("9月22日");
		//		RelativeSizeSpan sizeSpan = new RelativeSizeSpan(2.0f);
		//		spannableString.setSpan(sizeSpan, 0, 2, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

		mPaintSellBuy.setColor(mTitleTextColor);
		canvas.drawText("76.71", getWidth() / 2 - 200, getHeight() / 2, mPaintSellBuy);
		mPaintSellBuy.setColor(mTitleTextColor);
		canvas.drawText("76.92", getWidth() / 2 + 50, getHeight() / 2, mPaintSellBuy);

	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		int eventaction = event.getAction();
		switch (eventaction) {
		case MotionEvent.ACTION_DOWN:
			break;
		case MotionEvent.ACTION_MOVE: {

			break;
		}
		case MotionEvent.ACTION_UP: {
			int x = (int) event.getX();
			int y = (int) event.getY();

			if (x > 0 && x < 300 && y > 40 && y < 100) {
				Toast.makeText(context, "左侧卖出的区域,点击的xy的坐标分别为X" + x + ":" + y, Toast.LENGTH_SHORT).show();
			}
			if (x > 300 && x < 800 && y > 40 && y < 100) {
				Toast.makeText(context, "右侧买入的区域,点击的xy的坐标分别为X" + x + ":" + y, Toast.LENGTH_SHORT).show();
			}

			if (x > 210 && x < 380 && y > 0 && y < 40) {

				StationEventCaptain.getInstance().fireEventDataChange(
						new StationEventData(IStationEventName.MESSAGE_STATE_CHANGE, true));
				Toast.makeText(context, "中间商品的部分区域,点击的xy的坐标分别为X" + x + ":" + y, Toast.LENGTH_SHORT).show();
			}
			break;
		}
		}
		this.invalidate();
		return true;
	}

	//刷新商品的值
	public void setInstument() {
		System.out.println("是否可以直接调用这个方法");

	}

}
        主要使用到了回调接口,和自定义view的概念.完整的工程如下: android 自定义view 多区域点击事件

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值