创建自己的View类-移动的Picture

本文介绍如何在安卓开发中创建自定义View组件,并通过一个PicView类的实例展示如何响应触摸事件来更新图片位置。


   在安卓中,我们所用的UI组件一般都集成于基类android.view.View。在实际开发中,类库中的组件可能不能满足我们的需求,这时候就需要定义自己的View类了。

下面将会有一个实例:实现一个PicView类,用于显示一张图片,当触摸屏幕时,该图片就会出现在所触摸的地方。

首先,我们需要新建一个类,例如PicView,继承于android.view.View,重写onDraw()方法。

package com.example.movepic;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.View;
/**PicView类继承于android.view.View类
 * 构造函数初始化对象的位置
 * 重写onDraw函数显示图片*/
public class PicView extends View{
	public float bitmapX;
	public float bitmapY;
	public PicView(Context context) {
		super(context);
		// TODO 自动生成的构造函数存根
		bitmapX = 290;
		bitmapY = 140;
	}
	@Override
	protected void onDraw(Canvas c){
		super.onDraw(c);
		Paint paint = new Paint();
		Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.p2);
		c.drawBitmap(bitmap, bitmapX, bitmapY, paint);
		//判断图片是否被回收
		if(bitmap.isRecycled())
			bitmap.recycle();
	}

}
在MainActivity中新建PicView对象并设置监听事件。
package com.example.movepic;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.FrameLayout;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		/**构造帧布局管理器
		 * 新建PicView对象并初始化
		 * 设置该View对象的触摸监听事件
		 * 把该对象添加到布局管理器中*/
		FrameLayout frameLayout = (FrameLayout)findViewById(R.id.FrameLayout1);
		final PicView picView = new PicView(this);
		picView.setOnTouchListener(new OnTouchListener(){

			@Override
			public boolean onTouch(View v, MotionEvent e) {
				// TODO 自动生成的方法存根
				picView.bitmapX = e.getX();
				picView.bitmapY = e.getY();
				//重新绘制图片
				picView.invalidate();
				return false;
			}
			
		});
		frameLayout.addView(picView);
		
	}

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

}
这样即可定义并应用自己的View组件。


                
<view data-event-opts="{{[['touchmove',[['scrollhoming',['$event']]]],['touchend',[['scrollhomed',['$event']]]]]}}" class="glance-shop-cart" style="background-color:#F5F5F5;" bindtouchmove="__e" bindtouchend="__e"> <view style="height:10px;background-color:#F5F5F5;"></view> <block wx:if="{{shownullcart}}"> <view style="width:100%;height:250px;"> <view class="glance-shop-cart-nullcart"></view> <view style="height:30px;width:100%;font-size:15px;line-height:30px;text-align:center;">您的购物车为空,快去添加心爱的商品吧!</view> </view> </block> <block wx:for="{{$root.l0}}" wx:for-item="item" wx:for-index="index" wx:key="index"> <view style="background-color:#FFFFFF;"> <scroll-view style="width:100%;white-space:nowrap;" scroll-x="true" scroll-left="{{scrollposition}}" scroll-with-animation="true"> <view class="glance-shop-cart-scrollx-items" style="display:inline-block;width:100%;"> <view class="glance-shop-cart-scrollx-items-item"> <view style="width:10%;height:100%;background-color:#FFFFFF;"> <view data-event-opts="{{[['tap',[['clickitemselected',['$0'],[[['cart','',index,'id']]]]]]]}}" class="{{['glance-shop-cart-scrollx-items-item-sel',item.$orig.id>0?'glance-shop-cart-itemselected-img':'glance-shop-cart-itemunselected-img']}}" bindtap="__e"></view> </view> <view style="width:30%;height:100%;text-align:center;"> <image style="height:75px;width:75px;line-height:80px;padding-top:5px;" src="{{baseUrl+item.$orig.picture}}" mode="scaleToFill"></image> </view> <view class="glance-shop-cart-items-item-des"> <view class="sigle-line-text" style="font-size:16px;height:33.33%;text-align:left;">{{item.$orig.goodname}}</view> <view class="glance-shop-cart-items-item-pq"> <view class="sigle-line-text" style="font-size:15px;text-align:left;width:50%;">{{"¥"+item.g0}}</view> <view class="glance-shop-cart-items-item-opt"> <view data-event-opts="{{[['tap',[['minusitem',['$0'],[[['cart','',index,'id']]]]]]]}}" class="glance-shop-cart-items-item-opt-quantity-minus" bindtap="__e">-</view> <view class="glance-shop-cart-items-item-opt-quantity">{{item.$orig.buynumber}}</view> <view data-event-opts="{{[['tap',[['plusitem',['$0'],[[['cart','',index,'id']]]]]]]}}" class="glance-shop-cart-items-item-opt-quantity-plus" bindtap="__e">+</view> </view> </view> </view> </view> </view> </scroll-view> <view style="height:10px;background-color:#F5F5F5;"></view> </view> </block> <view class="glance-shop-cart-order"> <view style="width:12%;"> <view data-event-opts="{{[['tap',[['clickitemselectedall',['$event']]]]]}}" class="{{['glance-shop-cart-scrollx-items-item-sel',isselectedall?'glance-shop-cart-itemselected-img':'glance-shop-cart-itemunselected-img']}}" bindtap="__e"></view> </view> <view class="glance-shop-cart-total-cnt">{{"已选:( "+cntitems+" )"}}</view> <view class="glance-shop-cart-total-amt">{{"¥"+$root.g1}}</view> <view data-event-opts="{{[['tap',[['createorder',['$event']]]]]}}" class="glance-shop-cart-create-order" bindtap="__e">立即下单</view> </view> </view>详细解释这个代码
11-27
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值