簡體   English   中英

我想在屏幕上的不同位置隨機顯示圖像視圖

[英]I want to display the image view at different position on the screen randomly

這里的ImageView僅顯示在一個位置,下一次關閉該活動后,關閉該活動后, ImageView將位於另一個位置...我想在同一活動本身的diff位置隨機顯示ImageView 圖像視圖應該突然出現在一個點上,下一秒鍾ImageView應該從該位置消失並出現在下一個位置。 我該怎么做?

public class page2 extends ActionBarActivity {
ImageView b2;
int count = 0;
Handler handler = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_page2);

    Intent c = getIntent();
    String name = c.getStringExtra("t");
    Toast.makeText(getApplicationContext(), name, Toast.LENGTH_SHORT).show();


    b2 = (ImageView) findViewById(R.id.redball);

    AbsoluteLayout.LayoutParams absParams =
            (AbsoluteLayout.LayoutParams)b2.getLayoutParams();

    DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    int width = displaymetrics.widthPixels;
    int height = displaymetrics.heightPixels;


    Random r = new Random();

    absParams.x =  r.nextInt(width ) ;
    absParams.y =  r.nextInt(height );
    b2.setLayoutParams(absParams);

    Animation animation = AnimationUtils.loadAnimation(page2.this, R.anim.fade);
   // Animation animation1 = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.activity_move);


    b2.startAnimation(animation);
   // b2.startAnimation(animation1);
    b2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            count = count + 1;

        }
    });


    handler = new Handler();

    final Runnable t = new Runnable() {
        public void run() {

            Intent d = new Intent(getApplicationContext(), Page3.class);
            d.putExtra("count", count);
            d.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(d);


        }
    };

    handler.postDelayed(t, 4000);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_page2, menu);

    return true;
}

不要使用AbsoluteLayout ,為什么不使用自定義視圖繪制呢?

您可以通過在FrameLayout中使用Imageview來實現。 只需更改圖像的layoutParams即可更改其位置。

據我了解,您希望每次打開活動時,都不想真正向用戶查看ImageView移動的用戶,為什么要使用Animation? 您可以每次將ImageView動態地添加到活動中,並每次為其分配不同的Margin屬性。

    LinearLayout layout = (LinearLayout) view.findViewById(R.id.linear);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
            LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
    params.setMargins(0, 0, 10, 0); //substitute parameters for left, top, right, bottom
    ImageView iv = new ImageView(getActivity());
    iv.setLayoutParams(params);
    iv.setImageResource(R.drawable.yourimage);
    layout.addView(iv);

用於計算

ContainerHeight = blinkerContainer.getHeight(); // total height of screen
        ContainerWidth = blinkerContainer.getWidth(); //total width
        blinkerHeight = blinkView.getHeight();
        blinkerWidth = blinkView.getWidth();
        minTopMargin = 30;
        minLeftMargin = 30;

        maxTopMargin = ContainerHeight - blinkerHeight - 30;
        maxLeftMargin = ContainerWidth - blinkerWidth - 30;

用於定位

LinearLayout.LayoutParams params = (android.widget.LinearLayout.LayoutParams) blinkView
                    .getLayoutParams();
            params.leftMargin = minLeftMargin
                    + new Random().nextInt(maxLeftMargin - minLeftMargin);
            params.topMargin = minTopMargin
                    + new Random().nextInt(maxTopMargin - minTopMargin);

您可以使用AlaramManager進行計划

您的解決方案幾乎是正確的。 不幸的是,您似乎正在從計時器中重新啟動活動。 相反,您應該觸發重繪。

這個問題有兩個關於如何創建循環計時器的解決方案。 使用runOnUiThread()的解決方案應允許您執行隨機化並重新顯示ImageView

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM