当前位置:   article > 正文

android自定义view之---组合view_android 自定义组合view

android 自定义组合view

最近工作比较轻松,没有什么事情干,于是进入高产模式(呃。。。。高产似xx)。

应该很多童鞋对自定义view这个东西比较抵触,可能是听网上说view比较难吧,其实自定义view并没有很难

自定义view分为三种

1.自绘view

2.组合控件view

3.重写系统view

今天我们就来以一个小例子讲一下自定义view中的组合控件view,所谓的组合控件view就是使用系统预设的view来进行组合成一个新的view。并不进行图形的绘制操作。好了,今天的目标是把之前用Animation实现的loading动画做成一个view来使用,如果你还不了解Animation动画 可以去开开这篇博客:动画介绍--Animation 实现loading动画效果

老规矩,先上效果图:


可以看到效果跟上篇博客差不多,但是现在他可是一个view。我们首先来看一下布局文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent" android:layout_height="match_parent">
  4. <com.wingsoft.loadinganimation.LoadingView
  5. android:id="@+id/loading"
  6. android:layout_width="300dp"
  7. android:layout_centerInParent="true"
  8. android:layout_height="300dp">
  9. </com.wingsoft.loadinganimation.LoadingView>
  10. <Button
  11. android:id="@+id/button_cancel"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:text="cancel"/>
  15. </RelativeLayout>

这是主布局文件, 一个相对布局,一个按钮。 看到其中一个控件是带包名的,说明这个控件是我们自定义的控件。

由于我们今天介绍的是控件组合view,所以新建一个类让他继承于FrameLayout

  1. public class LoadingView extends FrameLayout {
  2. private ImageView mImageView;
  3. private TextView mTextView;
  4. private AnimationSet mImageAni = new AnimationSet(true);
  5. private AnimationSet mTextAni = new AnimationSet(true);
  6. public LoadingView(Context context, AttributeSet attrs) {
  7. super(context, attrs);
  8. LayoutInflater.from(context).inflate(R.layout.loading_view,this);
  9. mImageAni = new AnimationSet(true);
  10. mTextAni = new AnimationSet(true);
  11. mImageView = (ImageView) findViewById(R.id.loadingView_point);
  12. mTextView = (TextView) findViewById(R.id.loadingView_loading);
  13. TranslateAnimation ta = new TranslateAnimation(100, 0, 200, 0);
  14. ta.setDuration(5000);
  15. RotateAnimation ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
  16. ra.setDuration(5000);
  17. mImageAni.addAnimation(ta);
  18. mImageAni.addAnimation(ra);
  19. ScaleAnimation sa = new ScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
  20. sa.setDuration(5000);
  21. AlphaAnimation aa = new AlphaAnimation(0, 1);
  22. aa.setDuration(5000);
  23. mTextAni.addAnimation(sa);
  24. mTextAni.addAnimation(aa);
  25. aa.setAnimationListener(new Animation.AnimationListener() {
  26. @Override
  27. public void onAnimationStart(Animation animation) {
  28. }
  29. @Override
  30. public void onAnimationEnd(Animation animation) {
  31. mTextView.startAnimation(mTextAni);
  32. mImageView.startAnimation(mImageAni);
  33. }
  34. @Override
  35. public void onAnimationRepeat(Animation animation) {
  36. }
  37. });
  38. }
  39. public void start(){
  40. mTextView.startAnimation(mTextAni);
  41. mImageView.startAnimation(mImageAni);
  42. }
  43. public void stop(){
  44. mTextView.clearAnimation();
  45. mImageView.clearAnimation();
  46. }
  47. }

关键点在于 实现一个两个参数的构造器。在构造器里面调用LayoutInflater 将布局文件转换成view; 之后再给我们的view 添加两个方法 一个是start(),一个是stop() 用来控制动画是否播放。这样便完成了view的编写。下面在MainActivity中对他进行操作。

  1. public class MainActivity extends ActionBarActivity {
  2. private LoadingView mLoadingView;
  3. private Button mButton;
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_main);
  8. mLoadingView = (LoadingView) findViewById(R.id.loading);
  9. mButton = (Button)findViewById(R.id.button_cancel);
  10. mLoadingView.setOnClickListener(new View.OnClickListener() {
  11. @Override
  12. public void onClick(View view) {
  13. mLoadingView.start();
  14. }
  15. });
  16. mButton.setOnClickListener(new View.OnClickListener() {
  17. @Override
  18. public void onClick(View view) {
  19. mLoadingView.stop();
  20. }
  21. });
  22. }
  23. }

可以看到,我们可以像使用系统控件一样使用这个view了,怎么样,很神奇吧。你也快试试!之后我们会介绍自绘view。

源代码下载


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小舞很执着/article/detail/759760
推荐阅读
相关标签
  

闽ICP备14008679号