Andorid Support Design库 新控件 (下)

2 篇文章 0 订阅
5,android.support.design.widget.TabLayout 选项卡
通过选项卡的方式切换View并不是MD中才有的新概念,它们和顶层导航模式或者组织app中不同分组内容
(比如,不同风格的音乐)是同一个概念。 Design library的TabLayout 既实现了固定的选项卡(View的宽度平均分配),
也实现了可滚动的选项卡(View宽度不固定同时可以横向滚动)。如果你使用ViewPager在 tab之间横向切换,
你可以直接从PagerAdapter的getPageTitle() 中创建选项卡,然后使用setupWithViewPager()将两者联系在一起。

它可以使tab的选中事件能更新ViewPager,同时 ViewPager的页面改变能更新tab的选中状态。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.jht.administrator.day1024.TabLayoutActivity">
    <android.support.design.widget.TabLayout
        android:id="@+id/id_tablayout"
        android:layout_height="60dp"
        android:layout_width="match_parent"
        android:background="#ffff00"
        app:tabIndicatorColor="@color/colorAccent"
        app:tabIndicatorHeight="4dp"
        app:tabSelectedTextColor="@color/colorAccent"
        app:tabTextColor="#000000">

    </android.support.design.widget.TabLayout>
    <android.support.v4.view.ViewPager
        android:id="@+id/id_vp"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_below="@+id/id_tablayout">

    </android.support.v4.view.ViewPager>
</LinearLayout>
public class TabLayoutActivity extends AppCompatActivity {

    private TabLayout mTabLayout;
    private ViewPager mVp;
    private List<Fragment>fragments;
    private List<String>titles;
    private MyPagerAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab_layout);
        mTabLayout = (TabLayout) findViewById(R.id.id_tablayout);
        mVp = (ViewPager) findViewById(R.id.id_vp);
        initData();
        setData();
    }

    private void initData(){
        fragments = new ArrayList<>();
        titles = new ArrayList<>();
        for (int i = 0; i < 8; i++) {
            titles.add("第"+(i+1)+"项");
            MyFragment fragment = new MyFragment();
            Bundle bundle = new Bundle();
            bundle.putString("key","第"+(i+1)+"页内容");
            fragment.setArguments(bundle);

            fragments.add(fragment);
        }
        adapter = new MyPagerAdapter(getSupportFragmentManager(),fragments,titles);

    }

    private void setData(){
        mVp.setAdapter(adapter);
        //设置tablayout和viewpager一起联动,相关
        mTabLayout.setupWithViewPager(mVp);

        //设置tablayout的滑动模式
        mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
    }
}

public class MyPagerAdapter extends FragmentPagerAdapter{

    private List<Fragment>fragments;
    private List<String>titles;
    public MyPagerAdapter(FragmentManager fm,List<Fragment>fragments,List<String>titles) {
        super(fm);
        this.fragments = fragments;
        this.titles = titles;
    }

    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

    @Override
    public int getCount() {
        return fragments.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return titles.get(position);
    }
}



6.android.support.design.widget.CoordinatorLayout 超级FrameLayout
手势,及滚动布局,MD的手势有很多组成部分,包括touch ripples和meaningful transitions。
Design library引入了CoordinatorLayout,一个从另一层面去控制子view之间触摸事件的布局,
Design library中的很多控件都利用了它。一个很好的例子就是当你将FloatingActionButton作
为一个子View添加进 CoordinatorLayout并且将CoordinatorLayout传递给 Snackbar.make(),
在3.0及其以上的设备上,Snackbar不会显示在悬浮按钮的上面,而是FloatingActionButton 
利用CoordinatorLayout提供的回调方法,在Snackbar以动画效果进入的时候自动向上移动让出位置,
并且在Snackbar动画地消失的时候回到原来的位置,不需要额外的代码。


CoordinatorLayout的另一个用例是ActionBar与滚动技巧。你可能已经在自己的布局中使用了Toolbar
它允许你更加自由的自定义其外观与布局的其余部分融为一体。Design library把这种设计带到了更高的水平,
使用AppBarLayout可以让你的Toolbar与其他View(比如TabLayout的选项卡)能响应被标记了
ScrollingViewBehavior的View的滚动事件。
注意项:
当用户滚动RecyclerView,AppBarLayout可以这样响应滚动事件:
根据子view的滚动标志(scroll flag)来控制它们如何进入(滚入屏幕)与退出(滚出屏幕)。
Flag包括:
    scroll:所有想滚动出屏幕的View都需要设置这个flag,没有设置这个flag的View将被固定在屏幕顶部。
    enterAlways:这个flag让任意向下的滚动都会导致该View变为可见,启用快速“返回模式”。
    enterAlwaysCollapsed:当你的视图已经设置minHeight属性又使用此标志时,你的视图只能已最小高度进入,只有当滚动视图到达顶部时才扩大到完整高度。
    exitUntilCollapsed:this flag causes the view to scroll off until it is ‘collapsed’ (its minHeight) before exiting。


特别注意:所有使用scroll flag的View都必须定义在没有使用scroll flag的View前面,这样才能确保所有的View从顶部退出,留下固定的元素。
PS一句:CoordinatorLayout还提供了layout_anchor和layout_anchorGravity属性一起配合使用,可以用于放置floating view,比如FloatingActionButton与其他View的相对位置。相见Demo中演示。 




android.support.design.widget.AppBarLayout MD风格的滑动Layout
这个没啥解释的,就是一个ViewGroup,配合ToolBar与CollapsingToolbarLayout等使用。就是一个纯容器类。
无特殊注意项。 


android.support.design.widget.CollapsingToolbarLayout 可折叠MD风格ToolbarLayout 
可伸缩折叠的Toolbar (Collapsing Toolbar),直接添加Toolbar到AppBarLayout可以让你使用enterAlwaysCollapsed和 
exitUntilCollapsedscroll标志,但是无法控制不同元素如何响应collapsing的细节。这里使用了CollapsingToolbarLayout
的app:layout_collapseMode=”pin”来确保Toolbar在view折叠的时候仍然被固定在屏幕的顶部。还可以做到更好的效果,
当你让CollapsingToolbarLayout和Toolbar在一起使用的时候,title 会在展开的时候自动变得大些,而在折叠的时候让字
体过渡到默认值。必须注意,在这种情况下你必须在CollapsingToolbarLayout上调用 setTitle(),而不是在Toolbar上。
除了固定住View,你还可以使用 app:layout_collapseMode=”parallax”
(以及使用 app:layout_collapseParallaxMultiplier=”0.7”来设置视差因子)来实现视差滚动效果
(比如 CollapsingToolbarLayout里面的一个ImageView),这中情况和CollapsingToolbarLayout的 
app:contentScrim=”?attr/colorPrimary”属性一起配合更完美。
有一件事情必须注意,那就是CoordinatorLayout并不知道FloatingActionButton或者AppBarLayout的内部工作原理,
它只是以Coordinator.Behavior的形式提供了额外的API,该API可以使子View更好的控制触摸事件与手势以及声明
它们之间的依赖,并通过onDependentViewChanged()接收回调。
可以使用CoordinatorLayout.DefaultBehavior(你的View.Behavior.class)注解或者在布局中使用
app:layout_behavior=”com.example.app.你的View$Behavior”属性来定义view的默认行为。 
framework让任意View和CoordinatorLayout结合在一起成为了可能。 


nestedscrollview:

    <android.support.v7.widget.Toolbar
        android:id="@+id/id_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary">

    </android.support.v7.widget.Toolbar>
public class ToolbarBasicActivity extends AppCompatActivity {

    private Toolbar toolbar;
    Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        requestWindowFeature(Window.FEATURE_NO_TITLE);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_toolbar_basic);
        context = this;
        toolbar = (Toolbar) findViewById(R.id.id_toolbar);

        //设置导航栏的图标
        toolbar.setNavigationIcon(R.mipmap.menu);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("tag","导航栏被电击了");
            }
        });

        //设置app 的logo
        toolbar.setLogo(R.mipmap.ic_launcher);
        //设置标题
        toolbar.setTitle("Title");
        toolbar.setTitleTextColor(Color.WHITE);
        toolbar.setSubtitle("SubTitle");
        toolbar.setSubtitleTextColor(Color.GRAY);
        //在toolbar当中加载菜单内容
        toolbar.inflateMenu(R.menu.base_toolbar_menu);

        //设置菜单项对应的点击事件
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.action_setting:
                        Toast.makeText(context,"您点击了‘设置’选项",Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.action_search:
                        Toast.makeText(context,"您点击了‘搜索’选项",Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.action_notify:
                        Toast.makeText(context,"您点击了‘通知’选项",Toast.LENGTH_SHORT).show();
                        break;
                }

                return false;
            }
        });
    }
}


 <android.support.v4.widget.NestedScrollView
        android:id="@+id/id_nested_sv"
        android:layout_height="wrap_content"
        android:layout_width="match_parent">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:src="@mipmap/ic_launcher"
                android:scaleType="centerCrop"/>
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:src="@mipmap/ic_launcher"
                android:scaleType="centerCrop"/>
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:src="@mipmap/ic_launcher"
                android:scaleType="centerCrop"/>
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:src="@mipmap/ic_launcher"
                android:scaleType="centerCrop"/>
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:src="@mipmap/ic_launcher"
                android:scaleType="centerCrop"/>
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:src="@mipmap/ic_launcher"
                android:scaleType="centerCrop"/>
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:src="@mipmap/ic_launcher"
                android:scaleType="centerCrop"/>
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:src="@mipmap/ic_launcher"
                android:scaleType="centerCrop"/>
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:src="@mipmap/ic_launcher"
                android:scaleType="centerCrop"/>
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>

public class NestedActivity extends AppCompatActivity {

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



<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.design.widget.AppBarLayout
        android:id="@+id/id_appbar_layout"
        android:layout_width="match_parent"
        android:layout_height="80dp">
        <android.support.v7.widget.Toolbar
            app:title="1614班级的同学们都是好孩子"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/id_toolbar"
            app:layout_scrollFlags = "scroll|enterAlways"></android.support.v7.widget.Toolbar>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/id_nested_sv"
        android:layout_height="wrap_content"
        android:layout_width="match_parent">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:src="@mipmap/ic_launcher"
                android:scaleType="centerCrop"/>
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:src="@mipmap/ic_launcher"
                android:scaleType="centerCrop"/>
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:src="@mipmap/ic_launcher"
                android:scaleType="centerCrop"/>
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:src="@mipmap/ic_launcher"
                android:scaleType="centerCrop"/>
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:src="@mipmap/ic_launcher"
                android:scaleType="centerCrop"/>
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>


public class ComprehensiveActivity extends AppCompatActivity {

    private Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_comprehensive);
        toolbar = (Toolbar) findViewById(R.id.id_toolbar);
        //设置在actionbar的位置上显示toolbar
//        setSupportActionBar(toolbar);
    }



### 回答1: Android的USBCamera是一组用于在Android设备上与USB相机进行交互的API。这个是通过Android的USB设备框架来实现的。使用这个,开发人员可以获取USB相机的图像或视频数据,并将其用于实时预览或保存到设备存储器中。 使用USB相机的优势之一是它们可以在各种不同设备之间共享,而不需要额外的软件或驱动程序。与使用内置相机的设备相比,它们通常具有更高的分辨率和更大的存储容量。 USBCamera提供了一系列API和类,使开发人员能够实现各种与USB相机相关的功能,例如打开相机、设置分辨率和帧率、设置曝光和对焦等等。 另外,USBCamera也支持与其他Android和框架联合使用,例如OpenGL ES、OpenCV、TensorFlow等等。通过将数据流式传输到这些中的算法,可以实现更高级别的图像处理,例如目标检测、人脸识别等。 总的来说,Android的USBCamera使开发人员能够以更简单的方式与USB相机交互,从而使他们能够更轻松地实现高级别的图像处理应用。 ### 回答2: Android USB Camera是Android操作系统提供的一种机制,它允许开发人员与USB摄像头进行通信,并且内置了一些API来管理它们。它使得Android设备可以成为一个普通的USB摄像头,可以通过USB端口与计算机连接。最近的Android设备允许使用USB Type-C端口连接USB摄像头,这使得它们可以用作高质量的视频摄像头。 Android USB Camera的主要功能包括打开USB摄像头,捕获视频和静态图像,在捕获的过程中设置摄像头参数(例如曝光时间、帧速率等),以及对图像进行处理和渲染。它还提供了一个API来访问USB设备列表,并可以选择所需的摄像头。 通过使用Android USB Camera,开发人员可以创建基于USB摄像头的高质量视频应用程序,例如视频交流、监视和安防应用程序等。这使得Android设备成为了一个能够替代传统摄像头的全功能摄像机,为用户提供更具创意性和拍摄控制权的应用程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值