问题描述
- 怎么在ListView的Item里面去用SurfaceView绘制
-
package com.example.mysurfaceviewdemo;import java.util.Random;
import android.graphics.Bitmap;
public class BitmapBubble {
private Bitmap mBitmap; private int maxX; private int maxY; private int x; private int y; private boolean isRight = true; private boolean isUp = true; Random random = new Random(); private int speedX = random.nextInt(2)+1; private int speedY = random.nextInt(10)+1; public BitmapBubble(Bitmap mBitmap,int x,int y,int maxX,int maxY){ this.mBitmap = mBitmap; this.x = x; this.y = y; this.maxX = maxX - mBitmap.getWidth(); this.maxY = maxY - mBitmap.getHeight(); } public int getMaxX() { return maxX; } public void setMaxX(int maxX) { this.maxX = maxX; } public int getMaxY() { return maxY; } public void setMaxY(int maxY) { this.maxY = maxY; } public int getX() { x+=speedX; return x; } public int getY() { y-=speedY; return y; } public void setX(int x) { this.x = x; } public void setY(int y) { this.y = y; } public int getSpeedX() { return speedX; } public void setSpeedX(int speedX) { this.speedX = speedX; } public Bitmap getmBitmap() { return mBitmap; } public void setmBitmap(Bitmap mBitmap) { this.mBitmap = mBitmap; }
}
package com.example.mysurfaceviewdemo;
import java.util.ArrayList;
import java.util.Random;import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff.Mode;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;public class MySurfaceView extends SurfaceView implements Callback,Runnable{
private SurfaceHolder mSurfaceHolder; private Thread mThread; private Canvas canvas; private int ScreenW, ScreenH; private Random random = new Random(); private ArrayList <BitmapBubble> list = new ArrayList<BitmapBubble>(); int maxY; int maxX; int x ; int y ; private boolean hasBitmapBubble = true; private Bitmap mBitmap; private BitmapBubble mBitmapBubble; private int BitmapIds [] = new int []{R.drawable.water,R.drawable.water2,R.drawable.water3,R.drawable.water4}; public MySurfaceView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initSurfaceView(context); } public MySurfaceView(Context context, AttributeSet attrs) { super(context, attrs); initSurfaceView(context); } public MySurfaceView(Context context) { super(context); initSurfaceView(context); } @Override public void run(){ Canvas canvas = mSurfaceHolder.lockCanvas(); maxX = canvas.getWidth(); maxY = canvas.getHeight(); x = 30; y = maxY-80; mSurfaceHolder.unlockCanvasAndPost(canvas); while(true){ canvas = mSurfaceHolder.lockCanvas(); if(canvas==null){ return; } canvas.drawColor(Color.TRANSPARENT,Mode.CLEAR); for(int j=0; j<list.size();j++){ mBitmapBubble = list.get(j); canvas.drawBitmap(mBitmapBubble.getmBitmap(),mBitmapBubble.getX(), mBitmapBubble.getY(), null); if(mBitmapBubble.getX()>mBitmapBubble.getMaxX()){ list.remove(mBitmapBubble); } if(mBitmapBubble.getY()<0){ list.remove(mBitmapBubble); } } mSurfaceHolder.unlockCanvasAndPost(canvas); try { Thread.sleep(150); } catch (InterruptedException e) { e.printStackTrace(); } } } public void initSurfaceView(Context context){ mSurfaceHolder = this.getHolder(); mSurfaceHolder.addCallback(this); this.setZOrderOnTop(true);//设置画布 背景透明 this.getHolder().setFormat(PixelFormat.TRANSLUCENT); drawBubble(); mThread = new Thread(this); } @Override public void surfaceCreated(SurfaceHolder holder) { mThread.start(); //生产冒泡 new Thread(){ public void run() { while(true){ int position = random.nextInt(4); int resId = BitmapIds[position]; mBitmap = BitmapFactory.decodeResource(getResources(),resId); Bitmap bitmap = Bitmap.createScaledBitmap(mBitmap, 50, 50, false); list.add(new BitmapBubble(bitmap, x, y, maxX, maxY)); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } }; }.start(); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceDestroyed(SurfaceHolder holder) { mThread.stop(); } public ArrayList<BitmapBubble> getList() { return list; } public void setList(ArrayList<BitmapBubble> list) { this.list = list; } public void drawBubble(){ mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); Bitmap bitmap = Bitmap.createScaledBitmap(mBitmap, 40, 40, false); list.add(new BitmapBubble(bitmap, x, y, maxX, maxY)); }
}
package com.example.mysurfaceviewdemo;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;public class MyAdapter extends BaseAdapter{
Context mContext;
public MyAdapter(Context mContext){
this.mContext = mContext;
}@Override public int getCount() { return 10; } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { View view = LayoutInflater.from(mContext).inflate(R.layout.activity_main, null); final MySurfaceView mySurfaceView = (MySurfaceView)view.findViewById(R.id.mysurfaceview); Button btn = (Button) view.findViewById(R.id.btn); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mySurfaceView.drawBubble(); } }); return view; }
}
package com.example.mysurfaceviewdemo;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;public class MainActivity extends Activity {
MySurfaceView mySurfaceView;
Button btn;
ListView listview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
// setContentView(R.layout.activity_main);
// mySurfaceView = (MySurfaceView) findViewById(R.id.mysurfaceview);// btn = (Button) findViewById(R.id.btn);
// btn.setOnClickListener(new OnClickListener() {
// @Override
// public void onClick(View v) {
// mySurfaceView.drawBubble();
// }
// });listview = (ListView)findViewById(R.id.listview);
listview.setAdapter(new MyAdapter(getApplicationContext()));
}}
求大牛教育
,为什么我在绘制的时候,它的回调不走,但是SurfaceView的宿主是Activity的时候就可以去绘
解决方案
在ListView中多个item里面的按钮,获取不到当前item的位置
解决方案二:
http://www.linuxidc.com/Linux/2012-02/54369p4.htm