ホームに戻る
 スライドビュー

package org.example.test;

import android.app.*;
import android.content.*;
import android.graphics.*;
import android.os.*;
import android.view.*;
import android.view.View.*;
import android.view.animation.*;
import android.widget.*;

public class SlideView extends Activity
{
    private float x;
    private int width;
    private LinearLayout[] ll = new LinearLayout[2];
    private SampleView[] sv = new SampleView[2];
    private ViewFlipper vf;

    public void onCreate(Bundle savedInstanceState)
    {
      int i;

      super.onCreate(savedInstanceState);

      LinearLayout llp = new LinearLayout(this);
      llp.setOrientation(LinearLayout.VERTICAL);
      setContentView(llp);

      vf = new ViewFlipper(this);

      for(i = 0; i < ll.length; i++){
        ll[i] = new LinearLayout(this);
        sv[i] = new SampleView(this, i);
        ll[i].addView(sv[i]);
        vf.addView(ll[i]);
      }

      width = sv[0].getWidth();

      llp.addView(vf);
      setContentView(llp);

      llp.setOnTouchListener(new SampleTouchListener());
    }

    class SampleTouchListener implements OnTouchListener
    {
      public boolean onTouch(View v, MotionEvent e)
      {
        if(e.getAction() == MotionEvent.ACTION_DOWN)
        {
          x = e.getX();
        }
        else if(e.getAction() == MotionEvent.ACTION_UP)
        {
          if(x-20 > e.getX())
          {
            TranslateAnimation in = new TranslateAnimation(sv[0].getWidth(), 0, 0, 0);
            in.setDuration(100);
            TranslateAnimation out = new TranslateAnimation(0, -sv[0].getWidth(), 0, 0);
            out.setDuration(100);
            vf.setInAnimation(in);
            vf.setOutAnimation(out);
            vf.showNext();
          }
          else if(x+20 < e.getX())
          {
            TranslateAnimation in = new TranslateAnimation(-sv[0].getWidth(), 0, 0, 0);
            in.setDuration(100);
            TranslateAnimation out = new TranslateAnimation(0, sv[0].getWidth(), 0, 0);
            out.setDuration(100);
            vf.setInAnimation(in);
            vf.setOutAnimation(out);
            vf.showPrevious();
          }
        }

        return true;
      }
    }

    class SampleView extends View
    {
      private int num = 0;

      public SampleView(Context cn, int num)
      {
        super(cn);

        this.num = num;
      }

      protected void onDraw(Canvas cv)
      {
        super.onDraw(cv);
        Paint p = new Paint();
        if(num == 0){
          p.setColor(Color.argb(255, 255, 0, 0));
        }
        else{
          p.setColor(Color.argb(255, 0, 0, 255));
        }
        cv.drawCircle(10, 10, 10, p);
      }
    }
}

inserted by FC2 system