SevenCai7 2016-05-31 09:50 采纳率: 0%
浏览 1025

安卓UI相关问题,页卡下标Matrix矩阵无法初始化

http://blog.csdn.net/zdfghj123/article/details/51547781

具体写在了自己的博客里,希望有大牛看到帮我解答一下吧~

  • 写回答

1条回答 默认 最新

  • SevenCai7 2016-05-31 10:02
    关注

    具体达成上面这种效果,页面滑动,下标会改变,但是网络代码给出的matrix初始化下标的方法,下标一直是顶格在最左边的

    再附上自己的其余代码

    XML文件如下

     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffff" >
    
        <LinearLayout
            android:id="@+id/ll_tital"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_alignParentTop="true"
            android:background="@drawable/tab_bg"
            android:orientation="horizontal" >
    
            <Button
                android:id="@+id/btn_index"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="#00ffffff"
                android:text="首页"
                android:textColor="#fff" />
    
            <Button
                android:id="@+id/btn_classify"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="#00ffffff"
                android:text="分类"
                android:textColor="#fff" />
    
            <Button
                android:id="@+id/btn_rank"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="#00ffffff"
                android:text="排行"
                android:textColor="#fff" />
    
            <Button
                android:id="@+id/btn_manage"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="#00ffffff"
                android:text="管理"
                android:textColor="#fff" />
        </LinearLayout>
    
        <ImageView
            android:id="@+id/move_bar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/ll_tital"
            android:background="@drawable/switch_btn_on" />
    
        <android.support.v4.view.ViewPager
            android:id="@+id/id_viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/ll_tital" >
        </android.support.v4.view.ViewPager>
    
    </RelativeLayout>
    

    然后是主Activity

     package com.game.gamemarket;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import com.download.manager.MyDownloadManager;
    import com.frag.classifyfragment.ClassifyFragment;
    import com.frag.indexfragment.IndexFragment;
    import com.frag.managefragment.ManageFragment;
    import com.frag.rankfragment.RankFragment;
    import com.game.search.SearchGameInfo;
    
    import android.content.Intent;
    import android.graphics.BitmapFactory;
    import android.graphics.Matrix;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.view.ViewPager;
    import android.support.v4.view.ViewPager.OnPageChangeListener;
    import android.util.DisplayMetrics;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.Window;
    import android.view.animation.Animation;
    import android.view.animation.TranslateAnimation;
    import android.widget.Button;
    import android.widget.ImageButton;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    /**
     * 整体主框架
     */
    public class MainActivity extends FragmentActivity implements OnClickListener {
    
        // 跟随标题一起移动的
        private ImageView moveBarImg;
        // 导航的四个按钮
        private Button indexBtn;
        private Button classifyBtn;
        private Button rankBtn;
        private Button manageBtn;
    
        // 标题搜索栏按钮
        private TextView searchGameTitle;
        // 二维码扫描按钮
        private ImageButton quickMarkBtn;
        // 下载管理界面按钮
        private ImageButton dowmloadManagerBtn;
    
        // 作为页面容器的ViewPager
        private ViewPager mViewPager;
    
        // Fragment页面的集合
        private List<Fragment> fragmentList;
    
        // 四个Fragment界面
        private IndexFragment indexFragment;
        private ClassifyFragment classifyFragment;
        private RankFragment rankFragment;
        private ManageFragment manageFragment;
    
        private MyPagerAdapter myPagerAdapter;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
            setContentView(R.layout.activity_main);
            // 设置Titlebar
            getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);
    
            // 初始化数据
            init();
        }
    
        private void init() {
            indexBtn = (Button) findViewById(R.id.btn_index);
            classifyBtn = (Button) findViewById(R.id.btn_classify);
            rankBtn = (Button) findViewById(R.id.btn_rank);
            manageBtn = (Button) findViewById(R.id.btn_manage);
    
            searchGameTitle = (TextView) findViewById(R.id.title_search_text);
            quickMarkBtn = (ImageButton) findViewById(R.id.title_scan);
            dowmloadManagerBtn = (ImageButton) findViewById(R.id.title_download);
    
            // 初始化移动下标图片
            InitImageView();
    
            indexBtn.setOnClickListener(this);
            classifyBtn.setOnClickListener(this);
            rankBtn.setOnClickListener(this);
            manageBtn.setOnClickListener(this);
            searchGameTitle.setOnClickListener(this);
            quickMarkBtn.setOnClickListener(this);
            dowmloadManagerBtn.setOnClickListener(this);
    
            mViewPager = (ViewPager) findViewById(R.id.id_viewpager);
    
            fragmentList = new ArrayList<Fragment>();
            indexFragment = new IndexFragment();
            classifyFragment = new ClassifyFragment();
            rankFragment = new RankFragment();
            manageFragment = new ManageFragment();
    
            fragmentList.add(indexFragment);
            fragmentList.add(classifyFragment);
            fragmentList.add(rankFragment);
            fragmentList.add(manageFragment);
    
            myPagerAdapter = new MyPagerAdapter(getSupportFragmentManager(), fragmentList);
            mViewPager.setAdapter(myPagerAdapter);
            // 给ViewPager设置滑动改变监听
            mViewPager.setOnPageChangeListener(new MyOnPageChangeListener());
        }
    
        @Override
        public void onClick(View v) {
            Intent intent = null;
            switch (v.getId()) {
            case R.id.btn_index:
                changeView(0);
                break;
            case R.id.btn_classify:
                changeView(1);
                break;
            case R.id.btn_rank:
                changeView(2);
                break;
            case R.id.btn_manage:
                changeView(3);
                break;
            case R.id.title_search_text:// 进入搜索界面
                intent = new Intent(this, SearchGameInfo.class);
                startActivity(intent);
                break;
            case R.id.title_scan:// 进入二维码搜索
    
                break;
            case R.id.title_download:// 进入下载管理
                intent = new Intent(this, MyDownloadManager.class);
                startActivity(intent);
                break;
            }
        }
    
        // 手动设置ViewPager要显示的视图
        private void changeView(int desTab) {
            mViewPager.setCurrentItem(desTab, true);
        }
    
        private int offset = 0;// 动画图片偏移量
        private int bmpW;// 动画图片宽度
        private int currIndex = 0;// 当前页卡编号
    
        /**
         * 初始化动画,这个就是页卡滑动时,下面的横线也滑动的效果,在这里需要计算一些数据
         */
        private void InitImageView() {
            moveBarImg = (ImageView) findViewById(R.id.move_bar);
            // 获取图片宽度
            bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.switch_btn_on).getWidth();
            DisplayMetrics dm = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(dm);
            int screenW = dm.widthPixels;// 获取分辨率宽度
            offset = (screenW / 4 - bmpW) / 2;// 计算偏移量
    //      Matrix matrix = new Matrix();
    //      matrix.postTranslate(offset, 0);
    //      System.out.println(offset + "=====================================");
    //
    //      System.out.println(matrix);
    //      moveBarImg.setImageMatrix(matrix);// 设置动画初始位置
            moveBarImg.setX(offset);
    //       Animation animation = new TranslateAnimation(offset, offset, 0, 0);
    //       moveBarImg.startAnimation(animation);
    
        }
    
        public class MyOnPageChangeListener implements OnPageChangeListener {
    
            int one = offset * 2 + bmpW;// 页卡1 -> 页卡2 偏移量
            int two = one * 2;// 页卡1 -> 页卡3 偏移量
    
            public void onPageScrollStateChanged(int arg0) {
            }
    
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }
    
            public void onPageSelected(int arg0) {
                System.out.println("==============" + arg0 + "==============");
                Animation animation = new TranslateAnimation(one * currIndex, one * arg0, 0, 0);// 显然这个比较简洁,只有一行代码。
                currIndex = arg0;
                animation.setFillAfter(true);// True:图片停在动画结束位置
                animation.setDuration(300);
                moveBarImg.startAnimation(animation);
            }
        }
    }
    
    
    评论

报告相同问题?

悬赏问题

  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号