erji 采样

package com.example.yuekaomoni02;

import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.content.CursorLoader;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;

import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;

import com.example.yuekaomoni02.IconUtil.IconUtil;
import com.example.yuekaomoni02.adapter.FirstAdapter;
import com.example.yuekaomoni02.adapter.ListAdapter;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private DrawerLayout drawerLayout;
    private ViewPager main_viewPager;
    private TabLayout main_tabLayout;
    private ListView  main_loist;
    private ActionBarDrawerToggle toggle;
    private ListAdapter adapter;
    private ImageView imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取资源ID
        main_viewPager = findViewById(R.id.main_viewpager);
        main_tabLayout = findViewById(R.id.main_tablayout);
        main_loist = findViewById(R.id.main_list);
        drawerLayout = findViewById(R.id.drawer);

        //实例化一个 adapter
        adapter = new ListAdapter(this);
        //添加条目
        List<String> list = new ArrayList<>();
        list.add("首页");
        list.add("视频");
        list.add("我的");
        //添加
        adapter.setList(list);
        //设置适配器
        main_loist.setAdapter(adapter);

        initView();
        ImageData();
    }

    private void  initView(){
        //这个是左上角的n_按钮
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        drawerLayout = findViewById(R.id.drawer);
        toggle = new ActionBarDrawerToggle(this,drawerLayout,R.string.open_drawer,R.string.close_drawer);
        //设置按钮he 侧来绑定
        drawerLayout.addDrawerListener(toggle);
        //同步  开启状态
        toggle.syncState();

        //设置适配器
        main_viewPager.setAdapter(new FirstAdapter(getSupportFragmentManager()));
        main_tabLayout.setupWithViewPager(main_viewPager);
    }
       @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (toggle.onOptionsItemSelected(item)){
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    //二次采样
    public void  ImageData(){
        imageView = findViewById(R.id.main_image);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_PICK);
                intent.setType("image/*");
                startActivityForResult(intent,100);
            }
        });
    }
    public String uri2Path(Uri uri){

        String[] proj = {MediaStore.Images.Media.DATA};

        CursorLoader loader = new CursorLoader(this, uri, proj, null, null, null);

        Cursor cursor = loader.loadInBackground();

        int columnIndexOrThrow = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

        cursor.moveToFirst();

        return cursor.getString(columnIndexOrThrow);
    }

    private void progressImage(String imagePath){
        //得到图片的大小
        int dimensionPixelSize = getResources().getDimensionPixelSize(R.dimen.IConSize);
        //设置图片大小
        Bitmap bitmap = IconUtil.scaleBitmap(imagePath, dimensionPixelSize, dimensionPixelSize);
        //给图片赋值
        imageView.setImageBitmap(bitmap);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (requestCode == 100 && resultCode == RESULT_OK){
            Uri uri = data.getData();
            progressImage(uri2Path(uri));
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}

//IConUtil 类

package com.example.yuekaomoni02.IconUtil;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class IconUtil {
    //二次采样
    public static Bitmap scaleBitmap(String imagePath,int width,int height){
        //先得到BitmapFactory
        BitmapFactory.Options options = new BitmapFactory.Options();
        //只加载边框
        options.inJustDecodeBounds = true;
        //加载图片
        BitmapFactory.decodeFile(imagePath,options);
        //吧设置的大小比率  定义出来
        options.inSampleSize = Math.max(options.outWidth/width,options.outHeight/height);
        //加载像素
        options.inJustDecodeBounds = false;
        //返回
        return  BitmapFactory.decodeFile(imagePath,options);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值