現在位置と目的地から、現在位置から目的地を常に指すようなアプリの開発に取り組んでいます。
その表現のために、画像を回転させるのではなく右を向いた矢印の画像、左を向いた矢印の画像、などさまざまなパターンの矢印の画像を用意しそれをぱらぱら漫画のように画像を差し替えることで常に目的地の方向を向くようなプログラムを作ったのですが、
画像をCanvasで表示しているのですが、一度表示した画像が開放されていないようで、矢印が何個もあるように見えてしまうようになっています。画像を差し替えるときに一度表示したものは消すようにするにはどうしたらよろしいでしょうか。
下記に一部プログラムを記載しておきます
Android
1 2public class MainActivity extends SurfaceView implements SurfaceHolder.Callback, Runnable 3{ 4 5 private int gx; 6 private int gy; 7 private int ori; 8 private Paint paint; 9 private SurfaceHolder holder; 10 private Thread thread; 11 private int width; 12 private int height; 13 TextView textView; 14 public String gx2; 15 private Bitmap a,b,c,d,e,f,g,h,a1,a2,a3,a4,a5; 16 17 Calculate calc = new Calculate(); 18 double latitude1 = 35.625068; 19 double longitude1 = 139.343907; 20 double latitude2 = 35.625783; 21 double longitude2 = 139.345091; 22 int ans; 23 24 public MainActivity(Context context) 25 { 26 super(context); 27 holder = null; 28 thread = null; 29 paint = new Paint(); 30 paint.setAntiAlias(true); 31 gx = gy = 0; 32 a = BitmapFactory.decodeResource(getResources(), R.drawable.a); 33 b = BitmapFactory.decodeResource(getResources(), R.drawable.b); 34 c = BitmapFactory.decodeResource(getResources(), R.drawable.c); 35 d = BitmapFactory.decodeResource(getResources(), R.drawable.d); 36 e = BitmapFactory.decodeResource(getResources(), R.drawable.e); 37 f = BitmapFactory.decodeResource(getResources(), R.drawable.f); 38 g = BitmapFactory.decodeResource(getResources(), R.drawable.g); 39 h = BitmapFactory.decodeResource(getResources(), R.drawable.h); 40 a1 = BitmapFactory.decodeResource(getResources(), R.drawable.a1); 41 a2 = BitmapFactory.decodeResource(getResources(), R.drawable.a2); 42 a3 = BitmapFactory.decodeResource(getResources(), R.drawable.a3); 43 a4 = BitmapFactory.decodeResource(getResources(), R.drawable.a4); 44 a5 = BitmapFactory.decodeResource(getResources(), R.drawable.a5); 45 46 47 // getHolder()メソッドでSurfaceHolderを取得。さらにコールバックを登録 48 getHolder().addCallback(this); 49 } 50 51 //SurfaceView生成時に呼び出される 52 public void surfaceCreated(SurfaceHolder holder) 53 { 54 this.holder = holder; 55 thread = new Thread(this); 56// textView = (TextView)findViewById(R.id.textView); 57 ans = calc.getDirection(latitude1,longitude1,latitude2,longitude2); 58 } 59 60 //SurfaceView変更時に呼び出される 61 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) 62 { 63 if(thread != null ) 64 { 65 this.width = width; 66 this.height = height; 67 thread.start(); 68 } 69 70 } 71 72 //SurfaceView破棄時に呼び出される 73 public void surfaceDestroyed(SurfaceHolder holder) 74 { 75 thread = null; 76 } 77 78 //スレッドによるSurfaceView更新処理 79 public void run() 80 { 81 82 while (thread != null) 83 { 84 85 Log.d("aaaaaa",String.valueOf(ori)); 86 Canvas canvas = holder.lockCanvas(); 87// canvas.drawColor(Color.argb(255, 0, 0, 0)); 88 89// Log.d("aaaaaa",String.valueOf(gx2)); 90 91 if(gy >= -200 && gy <= 70 ) { 92 canvas.drawBitmap(a, 0, 600, paint); 93 94 } 95 if(gy > 70 && gy <= 140) { 96 canvas.drawBitmap(b, 0, 600, paint); 97 98 } 99 if(gy > 140 && gy <= 210) { 100 canvas.drawBitmap(c, 0, 600, paint); 101 102 } 103 if(gy > 210 && gy <= 280) { 104 canvas.drawBitmap(d, 0, 600, paint); 105 106 } 107 if(gy > 280 && gy <= 350) { 108 canvas.drawBitmap(e, 0, 600, paint); 109 110 } 111 if(gy > 350 && gy <= 420) { 112 canvas.drawBitmap(f, 0, 600, paint); 113 114 } 115 if(gy > 420 && gy <= 490) { 116 canvas.drawBitmap(g, 0, 600, paint); 117 118 } 119 else if(gy > 490 && gy <= 560){ 120 canvas.drawBitmap(h, 0, 600, paint); 121 } 122 123 124 125 126 127 if(gy >= -200 && gy <= 140 && ori <= 300+ans && ori >= 270+ans ) { 128 canvas.drawBitmap(a5, 0, 600, paint); 129 130 } 131 if(gy > -200 && gy <= 140 && ori > 300+ans && ori <= 360+ans ) { 132 canvas.drawBitmap(a4, 0, 600, paint); 133 134 } 135 if(gy > -200 && gy <= 140 && ori > 330+ans && ori <= 360+ans ) { 136 canvas.drawBitmap(a3, 0, 600, paint); 137 138 } 139 if(gy > -200 && gy <= 140 && ori > 0+ans && ori <= 60+ans) { 140 canvas.drawBitmap(a2, 0, 600, paint); 141 142 } 143 if(gy > -200 && gy <= 140 && ori > 60+ans && ori <= 90+ans) { 144 canvas.drawBitmap(a1, 0, 600, paint); 145 146 } 147 148 holder.unlockCanvasAndPost(canvas); 149 } 150 } 151 152 153 154 // 加速度の更新 155 public void setAcce(int gx, int gy) 156 { 157 this.gx = gx; 158 this.gy = gy; 159 this.gx2 = String.valueOf(gx); 160 } 161 162 //方位の更新 163 public void setOrientation(float ori) { 164 165 this.ori = (int)ori; 166 } 167}
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/01/12 11:14
退会済みユーザー
2017/01/12 12:50