2

レベルに分けられたシンプルなAndroidゲームを開発しています。レベルが完了すると、レベルボタン(レベル選択メニュー)の横にチェックアイコン(ImageView)を表示したいと思います。次のように、ボタンを押すとレベルが完了します(InsideLevelActivity):

    final EditText level1editText=(EditText)findViewById(R.id.level1editText);        
    Button level1completeButton=(Button)findViewById(R.id.level1completeButton);

    level1completeButton.setOnClickListener(new View.OnClickListener()
        public void onClick(View v)
        {
            final String edittext=level1editText.getText().toString();

            if(edittext.trim().equals("Complete level"))
             {   

              {
                Intent visible1 = new Intent();
                visible1.putExtra("iconvisible",0);
                startActivity(visible1);

                    {
                      LayoutInflater inflater = getLayoutInflater();
                      View view = inflater.inflate(R.layout.activity_level1completed,
                      (ViewGroup) findViewById(R.id.img11_toast));

                Toast toast = new Toast(Level1Activity.this);
                toast.setView(view);
                toast.show();

                { onBackPressed(); {

                        return;
                    }


            }   

            }
            else
            {
                    Toast.makeText(Level1Activity.this,
                    "Type Complete level.", Toast.LENGTH_LONG).show();
            }

その後、レベル選択メニューアクティビティに戻ります。私はこの方法でデータを取得しようとしています(LevelMenuActivity):

ImageView logocheckicon1=(ImageView)findViewById(R.id.logocheckicon1);
logocheckicon1.setVisibility(View.GONE);

Intent visible1 = getIntent();
int state = Integer.parseInt(visible1.getExtras().get("iconvisible").toString());
complete1.setVisibility(iconvisible);

私は過去数日間、これ(2つのアクティビティ間でデータを渡す方法)を含む多くのアプローチを試しました。チェックアイコン(ImageView)を非表示にし、この方法で再び表示できるようにすることも試みました。

また、完了したすべてのレベルの横に同じチェックアイコンが表示されます。(同じドローアブルの10個の異なるIDを作成せずに)1つのImageViewだけでこれを達成することは可能ですか?

前もって感謝します。

編集:私が十分に明確でなかったならば、私は謝罪します。たとえば、MainActivityにある画像の可視性を、別のアクティビティのボタン内のインテントで変更する方法があると思いました。ご回答ありがとうございます。

EDIT2:別の失敗した試行のコードを追加しました。

4

2 に答える 2

1

あるアクティビティから別のアクティビティに画像を渡すこと。最初に画像をビットマップに変換し、次にbase64に変換し、次に文字列を変換してから、インテントを介して渡すか、共有設定を保存します。

  public boolean saveImage(Context context, Bitmap realImage) {

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
         realImage.compress(Bitmap.CompressFormat.JPEG, 100, baos);   
         byte[] b = baos.toByteArray(); 

        String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);

    }

次に、インテントを介して別のアクティビティでこの画像を取得するか、共有設定から取得します

public Bitmap getFacebookImageBitmap(Context context)
{
     Bitmap bitmap = null;

     String saveimage=from intent or share-preference string.
     if( !saveimage.equalsIgnoreCase("") ){
            byte[] b = Base64.decode(saveimage, Base64.DEFAULT);
             bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);

        }
    return bitmap;
}

ありがとう

于 2012-10-24T03:41:58.210 に答える
1

インテントを介して画像を渡すことができます。まず、画像をバイト配列に変換し、意図して送信します。

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);     
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); 
byte[] b = baos.toByteArray();

Intent intent = new Intent(this, ActivityB.class);
intent.putExtra("picture", b);
startActivity(intent);

次に、次のアビティビティからこの画像を取得できます。

Bundle extras = getIntent().getExtras();
byte[] b = extras.getByteArray("picture");

Bitmap bmp = BitmapFactory.decodeByteArray(b, 0, b.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);

image.setImageBitmap(bmp);

これがお役に立てば幸いです。

于 2012-10-24T04:49:05.810 に答える