Android图片和视频简单选择器

本文介绍了在Android应用中如何实现图片和视频的选择。通过点击事件切换图片和视频显示,利用自定义主题和Fragment进行界面切换。同时,文章提到了共用一个Fragment类,并使用Picasso库展示图片, ThumbnailUtils获取视频缩略图。
摘要由CSDN通过智能技术生成

在实际的应用过程中图片和视频的显示使用异步加载

一、点击title时切换图片和视频

1、修改AndroidManifest.xml文件,使用自定义主题

 	<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/customTheme">
        <activity
            android:name="com.example.imageselector.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>


2、在onCreate方法中添加

	requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

	setContentView(R.layout.activity_main);

	// Set the header.
	getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
	R.layout.action_bar);


3、写布局文件

4、Photo和Video的点击事件,点击时切换fragment,调用switchFragment方法

	private void initFragment() {

		this.photoPickerFragment = new PhotoAndVideoPickerFragment();
		Bundle bundle = new Bundle();
		bundle.putInt("fragmentIndex", 0);
		this.photoPickerFragment.setArguments(bundle);

		this.videoPickerFragment = new PhotoAndVideoPickerFragment();
		Bundle videoBundle = new Bundle();
		videoBundle.putInt("fragmentIndex", 1);
		this.videoPickerFragment.setArguments(videoBundle);

		switchFragment(photoPickerFragment);
		this.currentFragment = this.photoPickerFragment;
	}

	private void switchFragment(PhotoAndVideoPickerFragment newFragment) {
		FragmentTransaction transaction = getSupportFragmentManager()
				.beginTransaction();
		if (null == this.currentFragment) {
			transaction.add(R.id.picker_fragment_container, newFragment)
					.commit();
		} else {
			if (currentFragment != newFragment) {
				if
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值