放置一个透明表面上查看地图 [英] Place a transparent surface view over a map
本文介绍了放置一个透明表面上查看地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发使用谷歌地图的Android应用程序,在此我需要把从表面上看在一张图上,这样我可以绘制地图上方的形状。我有DrawingSurface.java如下
进口android.content.Context;
进口android.graphics.Bitmap;
进口android.graphics.Canvas;
进口android.graphics.Paint;
进口android.graphics.PorterDuff;
进口android.util.AttributeSet;
进口android.util.Log;
进口android.view.SurfaceHolder;
进口android.view.SurfaceView;
进口的java.util.ArrayList;
进口java.util.Collections中;
进口java.util.Iterator的;
进口的java.util.List;公共类DrawingSurface扩展SurfaceView实现SurfaceHolder.Callback {
私人布尔_run;
保护DrawThread螺纹;
私人位图mBitmap;
公共布尔isDrawing = TRUE;
公共DrawingSurface(上下文的背景下,ATTRS的AttributeSet){
超(背景下,ATTRS); 。getHolder()的addCallback(本);
螺纹=新DrawThread(getHolder());
}
类DrawThread继承Thread {
私人SurfaceHolder mSurfaceHolder;
公共DrawThread(SurfaceHolder surfaceHolder){
mSurfaceHolder = surfaceHolder; } 公共无效setRunning(布尔运行){
_run =运行;
}
@覆盖
公共无效的run(){
帆布帆布= NULL;
而(_run){
如果(isDrawing ==真){
尝试{
帆布= mSurfaceHolder.lockCanvas(NULL);
如果(mBitmap == NULL){
mBitmap = Bitmap.createBitmap(1,1,Bitmap.Config.ARGB_8888);
}
最后帆布C =新的Canvas(mBitmap); c.drawColor(0,PorterDuff.Mode.CLEAR);
canvas.drawColor(0,PorterDuff.Mode.CLEAR);
canvas.drawBitmap(mBitmap,0,0,NULL);
} {最后
mSurfaceHolder.unlockCanvasAndPost(画布);
}
isDrawing = FALSE;
} } }
}
公共位图getBitmap(){
返回mBitmap;
}
公共无效surfaceChanged(SurfaceHolder架,INT格式,诠释的宽度,高度INT){
// TODO自动生成方法存根
mBitmap = Bitmap.createBitmap(宽度,高度,Bitmap.Config.ARGB_8888);;
}
公共无效surfaceCreated(SurfaceHolder持有人){
// TODO自动生成方法存根
thread.setRunning(真);
thread.start();
} 公共无效surfaceDestroyed(SurfaceHolder持有人){
// TODO自动生成方法存根
布尔重试= TRUE;
thread.setRunning(假);
而(重试){
尝试{
的Thread.join();
重试= FALSE;
}赶上(InterruptedException的E){
//我们会一次又一次地尝试...
}
}
}}
和铺陈为
<?XML版本=1.0编码=UTF-8&GT?;
<的FrameLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=垂直
机器人:ID =@ + ID /外行>
< com.google.android.maps.MapView
机器人:ID =@ + ID /图形页面
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:apiKey =0VD9w_GZnvQYr8v0KDuVHYn0DTw_ZXwqHnnP7CA
机器人:可点击=真/>
< com.experion.test.DrawingSurface
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:ID =@ + ID / surfaceView
/>< /&的FrameLayout GT;
我要吸取我的那段时间地图需要禁用滚动DrawingSurface形状。
我是否需要用帆布?
在此先感谢
解决方案
drawingSurface =(DrawingSurface)findViewById(R.id.drawingSurface);
drawingSurface.setZOrderOnTop(真); //必要
SurfaceHolder drawingSurface1 = drawingSurface.getHolder();
drawingSurface1.setFormat(PixelFormat.TRANSPARENT);
I am developing an android application using google map, in this i need to place a surface view over a map so that i can draw shapes above the map. i have DrawingSurface.java as below
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class DrawingSurface extends SurfaceView implements SurfaceHolder.Callback {
private Boolean _run;
protected DrawThread thread;
private Bitmap mBitmap;
public boolean isDrawing = true;
public DrawingSurface(Context context, AttributeSet attrs) {
super(context, attrs);
getHolder().addCallback(this);
thread = new DrawThread(getHolder());
}
class DrawThread extends Thread{
private SurfaceHolder mSurfaceHolder;
public DrawThread(SurfaceHolder surfaceHolder){
mSurfaceHolder = surfaceHolder;
}
public void setRunning(boolean run) {
_run = run;
}
@Override
public void run() {
Canvas canvas = null;
while (_run){
if(isDrawing == true){
try{
canvas = mSurfaceHolder.lockCanvas(null);
if(mBitmap == null){
mBitmap = Bitmap.createBitmap (1, 1, Bitmap.Config.ARGB_8888);
}
final Canvas c = new Canvas (mBitmap);
c.drawColor(0, PorterDuff.Mode.CLEAR);
canvas.drawColor(0, PorterDuff.Mode.CLEAR);
canvas.drawBitmap (mBitmap, 0, 0,null);
} finally {
mSurfaceHolder.unlockCanvasAndPost(canvas);
}
isDrawing = false;
}
}
}
}
public Bitmap getBitmap(){
return mBitmap;
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// TODO Auto-generated method stub
mBitmap = Bitmap.createBitmap (width, height, Bitmap.Config.ARGB_8888);;
}
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
thread.setRunning(true);
thread.start();
}
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
boolean retry = true;
thread.setRunning(false);
while (retry) {
try {
thread.join();
retry = false;
} catch (InterruptedException e) {
// we will try it again and again...
}
}
}
}
and the lay out as
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/lay">
<com.google.android.maps.MapView
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="0VD9w_GZnvQYr8v0KDuVHYn0DTw_ZXwqHnnP7CA"
android:clickable="true" />
<com.experion.test.DrawingSurface
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/surfaceView"
/>
</FrameLayout>
i need to draw shapes on my DrawingSurface on that time the map need to disable the scroll.
Whether i need to use canvas??
thanks in advance
解决方案
drawingSurface = (DrawingSurface) findViewById(R.id.drawingSurface);
drawingSurface.setZOrderOnTop(true); // necessary
SurfaceHolder drawingSurface1 = drawingSurface.getHolder();
drawingSurface1.setFormat(PixelFormat.TRANSPARENT);
这篇关于放置一个透明表面上查看地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文