2017-05-19 9 views
0

私は、.xmlレイアウトファイルを使用するのではなく、キャンバスで画面に描画するためにsurfaceViewを実装する.javaを使用していますが、何らかの点でどうしたらよいかを知りたいと思います。ビューが完了している)、このビューをレイアウトファイルに関連付けたり、ボタンまたはアラートダイアログを呼び出すことができます。キャンバスでAlertDialogを使用する

「あなたが負ける」のようなalerDialogを表示するためにゲームに勝ったり失敗したりするときのように、より明確になるようにします。

Main_Activityは、次のようになります。

public class Main extends Activity { 

    activity_layout_animation animation; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 

      animation = new activity_layout_animation(this); 
      setContentView(animation); 
     } 

     @Override 
     protected void onPause(){ 
      super.onPause(); 
      animation.pause(); 
     } 

     @Override 
     protected void onResume(){ 
      super.onResume(); 
      animation.resume(); 
      } 

ビューファイルのコードのいくつかの作品:

public class activity_layout_animation extends SurfaceView implements Runnable { 

boolean CanDraw = false 
     public activity_layout_animation(Context context){ 
      super(context); 
      surfaceHolder = getHolder(); 
    } 
     @Override 
     public void run(){ 
    while(CanDraw){ 

      if (!surfaceHolder.getSurface().isValid()){ 
       continue; 
      } 
    } 
} 

編集:私はこのようなsomethigを追加することができます。

AlertDialog.Builder builder; 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
     builder = new AlertDialog.Builder(context, android.R.style.Theme_Material_Dialog_Alert); 
    } else { 
     builder = new AlertDialog.Builder(context); 
    } 
    builder.setTitle("Delete entry") 
    .setMessage("Are you sure you want to delete this entry?") 
    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
      // continue with delete 
     } 
    }) 
    .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
      // do nothing 
     } 
    }) 
    .setIcon(android.R.drawable.ic_dialog_alert) 
    .show(); 

それだけで動作しますpublic activity_layout_animation(Context context){で追加したいrun()

+0

AlertDialogは、XMLからコンテンツビューをロードするか、コード内にコンテンツを作成するかに関係なく、正常に動作するはずです。 AlertDialogを呼び出してみましたか? – cyanide

答えて

-1
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context); 
     LayoutInflater inflater = activity.getLayoutInflater(); 
     final View dialogCoordinate = inflater.inflate(R.layout.show_coordinates, null); 
     dialogBuilder.setCancelable(false); 
     dialogBuilder.setView(dialogCoordinate); 

     ImageView imgShowCoordinates = (ImageView) dialogCoordinate.findViewById(R.id.imgShowCoordinates); 
     imgShowCoordinates.setImageResource(R.drawable.whitetrans); 
//  int width = dmsUtility.getScreenSize(activity)[0]; 
//  int height = dmsUtility.getScreenSize(activity)[1]; 
     Log.d("Coordinate", "showCoordinates: "+arrayList.get(pos).getComment()); 

     if (arrayList.get(pos).getComment() != null && !arrayList.get(pos).getComment().isEmpty() 
       && !arrayList.get(pos).getComment().equals("null")) { 
      String[] parts1 = arrayList.get(pos).getComment().split("[\\s\\,]+"); 
      Log.d(TAG, "getBody: " + arrayList.get(pos).getComment()); 
      float[] numbers = new float[parts1.length]; 

      for (int p = 0; p < numbers.length; p++) { 
       numbers[p] = Math.round(Float.parseFloat(parts1[p]) * 1000)/1000f; 
      } 

      bmp = ((BitmapDrawable) imgShowCoordinates.getDrawable()).getBitmap(); 
      if (bmp != null && !bmp.isRecycled()) { 
       Bitmap tempBitmap = Bitmap.createScaledBitmap(bmp, 300, 300, true); 
       tempCanvas = new Canvas(tempBitmap); 
       canW = tempBitmap.getWidth(); 
       canH = tempBitmap.getHeight(); 

       for (int q = 0; q < numbers.length; q = q + 4) { 
        float x1 = (canW/300) * numbers[0 + q]; 
        if (numbers.length > q + 2) { 
         Paint paint = new Paint(Paint.LINEAR_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG); 
         paint.setColor(Color.BLUE); // Text Color 
         paint.setStrokeWidth(1f); // Text Size 
         paint.setAntiAlias(true); 
         float y1 = (canH/300) * numbers[1 + q]; 
         float x2 = (canW/300) * numbers[2 + q]; 
         float y2 = (canH/300) * numbers[3 + q]; 
         tempCanvas.drawLine(x1, y1, x2, y2, paint); 
        } 
       } 
      } 
     } 

     dialogBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int whichButton) { 
       dialog.dismiss(); 
      } 
     }); 
     AlertDialog alertDialog = dialogBuilder.create(); 
     alertDialog.show(); 
+0

説明なしでいくつかのコードを投げるだけで良い習慣とはみなされません。コードに説明を追加してください。 –

関連する問題