1. 程式人生 > >點選頭像二次取樣

點選頭像二次取樣

1.側滑佈局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:orientation="vertical">
    <ImageView
        android:id="@+id/image111"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@mipmap/ic_launcher"/>
    <ListView
        android:id="@+id/menus"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>

</LinearLayout>

2.text

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"

    android:layout_height="match_parent">
    <TextView
        android:id="@+id/text"
        android:gravity="center"
        android:layout_width="200dp"
        android:layout_height="200dp" />


</LinearLayout>

3.Adapter

package com.bw.ymy.zonghe1.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bw.ymy.zonghe1.R;

public class leftAdapter extends BaseAdapter {
    private Context context;

    //z左邊展示資料
    private  String[] menus=new String[]
            {
            "我的","電影","首頁"
            };

    public leftAdapter(Context context) {
        this.context = context;
    }
    private final int ITEM_COUTN = 2;
    private final int IMAGE_TYPE = 0;
    private final int TEXT_TYPE = 1;


    @Override
    public int getCount() {
        return menus.length+1;
    }

    @Override
    public int getViewTypeCount() {
        return ITEM_COUTN;
    }

    @Override
    public int getItemViewType(int position) {
        return position==0?IMAGE_TYPE:TEXT_TYPE;
    }

    @Override
    public String getItem(int position) {
       if(position==0)
       {
           return  null;
       }
       return menus[position-1];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        if(convertView==null)
        {
            convertView=LayoutInflater.from(context).inflate(R.layout.lefttext,parent,false);
            holder=new ViewHolder(convertView);
        }else
        {
            holder = (ViewHolder) convertView.getTag();
        }
        if(getItemViewType(position)==TEXT_TYPE)
        {
            holder.bind(getItem(position));
        }
        return convertView;
    }
    //建立類
    class  ViewHolder{
        TextView  text;
        ImageView icon;
        public ViewHolder(View convertView)
        {
            icon=convertView.findViewById(R.id.icon);
            text=convertView.findViewById(R.id.text);
        }
        //賦值
        public  void  bind(String item)
        {
            text.setText(item);
        }
    }
}

4.Fragment

package com.bw.ymy.zonghe1.Fragment;

import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.content.CursorLoader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.bw.ymy.zonghe1.Adapter.leftAdapter;
import com.bw.ymy.zonghe1.MainActivity;
import com.bw.ymy.zonghe1.R;
import com.bw.ymy.zonghe1.nutil.Bitmap;

public class leftfragment extends Fragment {
    private ListView listview;
    private  leftAdapter adapter;
    private ImageView image11;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.leftfragment,container,false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //獲取資源id
        listview=view.findViewById(R.id.menus);
        image11=view.findViewById(R.id.image111);
        listview.setAdapter(new leftAdapter(getActivity()));


       image11.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Intent intent=new Intent(Intent.ACTION_PICK);
               intent.setType("image/*");
               startActivityForResult(intent,100);
           }
       });

        //點選跳轉
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                    ((MainActivity)getActivity()).shpwpage(position-1);
            }
        });

    }
    private  String url2path(Uri uri)
    {
        String[] proj={MediaStore.Images.Media.DATA};
        CursorLoader loader=new CursorLoader(getActivity(),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 s)
    {
        int size=getResources().getDimensionPixelSize(R.dimen.leftIconSize);
        android.graphics.Bitmap bitmap = Bitmap.scaleBitmap(s, size, size);
        image11.setImageBitmap(bitmap);




    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 100) {
            Uri uri = data.getData();
            progressimage(url2path(uri));
            return;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }


}

5.二次取樣

package com.bw.ymy.zonghe1.nutil;

import android.graphics.BitmapFactory;

public class Bitmap {


    public  static android.graphics.Bitmap scaleBitmap(String imagepath, int width, int height)
    {
        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);

    }
}

6.App

package com.bw.ymy.zonghe1.nutil;

import android.app.Application;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

public class App extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        ImageLoaderConfiguration configuration=null;
        DisplayImageOptions options=new DisplayImageOptions.Builder().build();
        configuration=new ImageLoaderConfiguration.Builder(this)
                .diskCacheSize(50*1024*1024)
                .build();
        ImageLoader.getInstance().init(configuration);
    }
}