大家好,我是 android 的初学者,我对访问变量有疑问.. 在我的应用程序中,我必须显示可绘制的上一个和下一个图像.. 它已经完成了.. 但我的问题是,它应该播放视频根据子类中的图像在父类中,所以我需要访问父类中的子类变量....是否可能?任何想法..谢谢!
// Main class
public class TransitionViewExampleActivity extends Activity implements
OnClickListener {
ImageButton play;
private final int ANIMATION_DURATION_MSEC = 1000;
private Button _leftButton;
private Button _rightButton;
private TransitionView _mainView;
VideoView videoView;
Context context;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.main);
play = (ImageButton) findViewById(R.id.ib_play_btn);
play.setOnClickListener(this);
_leftButton = (Button) findViewById(R.id.buttonLeft);
_rightButton = (Button) findViewById(R.id.buttonRight);
_mainView = (TransitionView) findViewById(R.id.mainView);
_mainView.setVisibility(View.VISIBLE);
}
public void onClick(View v) {
videoView = (VideoView) findViewById(R.id.videoView1);
switch (v.getId()) {
// TODO Auto-generated method stub
case R.id.ib_play_btn:
// Toast.makeText(this, "Please Login to view video",Toast.LENGTH_SHORT).show();
play.setVisibility(View.INVISIBLE);
_mainView.setVisibility(View.INVISIBLE);
_leftButton.setVisibility(View.INVISIBLE);
_rightButton.setVisibility(View.INVISIBLE);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
String path = "android.resource://" + getPackageName() + "/" + R.raw.test1;
videoView.setVideoURI(Uri.parse(path));
videoView.start();
break;
}
if (v == _leftButton) {
// fadeIn.setDuration(ANIMATION_DURATION_MSEC);
this.overridePendingTransition(R.anim.slide_in_left,
R.anim.slide_out_right);
_mainView.changePage(false);
} else if (v == _rightButton) {
this.overridePendingTransition(R.anim.slide_in_right,
R.anim.slide_out_left);
_mainView.changePage(true);
}
}
}
// Sub class (Child class)
class TransitionView extends RelativeLayout {
/** One of the two in-memory art images */
private ImageView _artView1;
/** The other of the two in-memory art images */
private ImageView _artView2;
/** Length of art view transition animation, in milliseconds */
private final int ANIMATION_DURATION_MSEC = 1000;
/** The underlying ImageSwitcher that performs transitions */
private ImageSwitcher _imageSwitcher;
/** Index into _imageIds array */
private int _currentImage = 0;
/** All available art image resource ids */
private final Integer[] _imageIds = { R.drawable.thuppaki,
R.drawable.gouravam, R.drawable.splash2, R.drawable.pic04 };
Animation fadeIn, fadeOut;
Context context;
public TransitionView(Context context) {
super(context);
customInit(context);
}
private void customInit(Context context) {
_imageSwitcher = new ImageSwitcher(context);
_imageSwitcher.setInAnimation(fadeIn);
_imageSwitcher.setOutAnimation(fadeOut);
_artView1 = new ImageView(context);
_artView1.setImageResource(_imageIds[_currentImage]);
_artView2 = new ImageView(context);
_artView2.setImageResource(_imageIds[_currentImage + 1]);
LayoutParams fullScreenLayout = new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
_imageSwitcher.addView(_artView1, 0, fullScreenLayout);
_imageSwitcher.addView(_artView2, 1, fullScreenLayout);
_imageSwitcher.setDisplayedChild(0);
addView(_imageSwitcher, fullScreenLayout);
}
/** @see android.view.View#View(Context, AttributeSet) */
public TransitionView(Context context, AttributeSet attrs) {
super(context, attrs);
customInit(context);
}
/** @see android.view.View#View(Context, AttributeSet, int) */
public TransitionView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
customInit(context);
}
public void changePage(boolean pageRight) {
_currentImage = (pageRight) ? (_currentImage + 1) : (_currentImage - 1);
if (_currentImage < 0) {
_currentImage = _imageIds.length - 1;
} else if (_currentImage >= _imageIds.length) {
_currentImage = 0;
}
if (_imageSwitcher.getCurrentView() == _artView1) {
_artView2.setImageResource(_imageIds[_currentImage]);
_imageSwitcher.showNext();
} else {
_artView1.setImageResource(_imageIds[_currentImage]);
_imageSwitcher.showPrevious();
}
}
}
最后,我想访问父类中的子类变量_imageIds和 _currentImage ........知道吗???