From 53a1f6bd0f35df1841a3cb41a37296e69c908ef4 Mon Sep 17 00:00:00 2001 From: wasabeef Date: Mon, 12 Jan 2015 00:20:14 +0900 Subject: [PATCH] update samples --- example/build.gradle | 1 + .../wasabeef/example/glide/MainActivity.java | 42 ++++--- .../wasabeef/example/glide/MainAdapter.java | 103 ++++++++++++++++++ example/src/main/res/layout/activity_main.xml | 16 +-- .../src/main/res/layout/layout_list_item.xml | 14 +++ 5 files changed, 146 insertions(+), 30 deletions(-) create mode 100644 example/src/main/java/jp/wasabeef/example/glide/MainAdapter.java create mode 100644 example/src/main/res/layout/layout_list_item.xml diff --git a/example/build.gradle b/example/build.gradle index fa6e985..e1130ef 100644 --- a/example/build.gradle +++ b/example/build.gradle @@ -52,5 +52,6 @@ def getKeyAliasPasswordProperty() { dependencies { compile project(':transformations') compile 'com.android.support:appcompat-v7:+' + compile 'com.android.support:recyclerview-v7:21.+' compile 'com.github.bumptech.glide:glide:3.+' } diff --git a/example/src/main/java/jp/wasabeef/example/glide/MainActivity.java b/example/src/main/java/jp/wasabeef/example/glide/MainActivity.java index b5fe760..e693e3c 100644 --- a/example/src/main/java/jp/wasabeef/example/glide/MainActivity.java +++ b/example/src/main/java/jp/wasabeef/example/glide/MainActivity.java @@ -1,9 +1,14 @@ package jp.wasabeef.example.glide; -import android.support.v7.app.ActionBarActivity; import android.os.Bundle; -import android.view.Menu; -import android.view.MenuItem; +import android.support.v7.app.ActionBarActivity; +import android.support.v7.widget.GridLayoutManager; +import android.support.v7.widget.RecyclerView; + +import java.util.ArrayList; +import java.util.List; + +import jp.wasabeef.example.glide.MainAdapter.Type; public class MainActivity extends ActionBarActivity { @@ -12,28 +17,19 @@ public class MainActivity extends ActionBarActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - } + RecyclerView recyclerView = (RecyclerView) findViewById(R.id.list); + recyclerView.setLayoutManager(new GridLayoutManager(this, 2)); - @Override - public boolean onCreateOptionsMenu(Menu menu) { - // Inflate the menu; this adds items to the action bar if it is present. - getMenuInflater().inflate(R.menu.menu_main, menu); - return true; - } + List dataSet = new ArrayList<>(); + dataSet.add(Type.Crop); + dataSet.add(Type.CropSquare); + dataSet.add(Type.CropCircle); + dataSet.add(Type.ColorFilter); + dataSet.add(Type.Grayscale); + dataSet.add(Type.RoundedCorners); + dataSet.add(Type.Blur); - @Override - public boolean onOptionsItemSelected(MenuItem item) { - // Handle action bar item clicks here. The action bar will - // automatically handle clicks on the Home/Up button, so long - // as you specify a parent activity in AndroidManifest.xml. - int id = item.getItemId(); - - //noinspection SimplifiableIfStatement - if (id == R.id.action_settings) { - return true; - } - - return super.onOptionsItemSelected(item); + recyclerView.setAdapter(new MainAdapter(this, dataSet)); } } diff --git a/example/src/main/java/jp/wasabeef/example/glide/MainAdapter.java b/example/src/main/java/jp/wasabeef/example/glide/MainAdapter.java new file mode 100644 index 0000000..1bb99d3 --- /dev/null +++ b/example/src/main/java/jp/wasabeef/example/glide/MainAdapter.java @@ -0,0 +1,103 @@ +package jp.wasabeef.example.glide; + +import com.bumptech.glide.Glide; +import com.bumptech.glide.load.Transformation; +import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool; + +import android.content.Context; +import android.graphics.Bitmap; +import android.graphics.Color; +import android.support.v7.widget.RecyclerView; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; + +import java.util.List; + +import jp.wasabeef.glide.transformations.BlurTransformation; +import jp.wasabeef.glide.transformations.ColorFilterTransformation; +import jp.wasabeef.glide.transformations.CropCircleTransformation; +import jp.wasabeef.glide.transformations.CropSquareTransformation; +import jp.wasabeef.glide.transformations.CropTransformation; +import jp.wasabeef.glide.transformations.GrayscaleTransformation; +import jp.wasabeef.glide.transformations.RoundedCornersTransformation; + +/** + * Created by Wasabeef on 2015/01/11. + */ +public class MainAdapter extends RecyclerView.Adapter { + + private Context mContext; + private List mDataSet; + private BitmapPool mPool; + + enum Type { + Crop, + CropSquare, + CropCircle, + ColorFilter, + Grayscale, + RoundedCorners, + Blur + } + + public MainAdapter(Context context, List dataSet) { + mContext = context; + mDataSet = dataSet; + mPool = Glide.get(mContext).getBitmapPool(); + } + + @Override + public MainAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { + View v = LayoutInflater.from(mContext) + .inflate(R.layout.layout_list_item, parent, false); + return new ViewHolder(v); + } + + @Override + public void onBindViewHolder(MainAdapter.ViewHolder holder, int position) { + Transformation transformation = null; + switch (mDataSet.get(position)) { + case Crop: + transformation = new CropTransformation(mPool, 300, 100); + break; + case CropSquare: + transformation = new CropSquareTransformation(mPool); + break; + case CropCircle: + transformation = new CropCircleTransformation(mPool); + break; + case ColorFilter: + transformation = new ColorFilterTransformation(mPool, Color.argb(80, 255, 0, 0)); + break; + case Grayscale: + transformation = new GrayscaleTransformation(mPool); + break; + case RoundedCorners: + transformation = new RoundedCornersTransformation(mPool, 50, 0); + break; + case Blur: + transformation = new BlurTransformation(mContext, mPool, 10); + break; + } + + Glide.with(mContext).load(R.drawable.demo) + .bitmapTransform(transformation).into(holder.image); + } + + @Override + public int getItemCount() { + return mDataSet.size(); + } + + static class ViewHolder extends RecyclerView.ViewHolder { + + public ImageView image; + + ViewHolder(View itemView) { + super(itemView); + image = (ImageView) itemView.findViewById(R.id.image); + } + } +} diff --git a/example/src/main/res/layout/activity_main.xml b/example/src/main/res/layout/activity_main.xml index 3d4f41c..ccb1aeb 100644 --- a/example/src/main/res/layout/activity_main.xml +++ b/example/src/main/res/layout/activity_main.xml @@ -1,11 +1,13 @@ + xmlns:tools="http://schemas.android.com/tools" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:background="#CC000000" + tools:context=".MainActivity"> - + diff --git a/example/src/main/res/layout/layout_list_item.xml b/example/src/main/res/layout/layout_list_item.xml new file mode 100644 index 0000000..4297dcf --- /dev/null +++ b/example/src/main/res/layout/layout_list_item.xml @@ -0,0 +1,14 @@ + + + + + + \ No newline at end of file