Skip to content

Instantly share code, notes, and snippets.

@lon9
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lon9/b668f81344fe81d7abf2 to your computer and use it in GitHub Desktop.
Save lon9/b668f81344fe81d7abf2 to your computer and use it in GitHub Desktop.
AndroidでYouTubeのサムネイルをアス比を直して表示する。 ref: http://qiita.com/Rompei/items/46ae14a49b7a383153db
public class CustomAdapter extends ArrayAdapter<VideoItems> {
private LayoutInflater layoutInflater;
private Context context;
public CustomAdapter(Context context, int textViewResourceId, List<VideoItems> objects, RequestQueue queue) {
super(context, textViewResourceId, objects);
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.context = context;
}
static class ViewHolder {
ImageView image;
TextView numView;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
VideoItems videoItems = getItem(position);
String thumbnailurl = videoItems.snippet.thumbnails.high.url;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.list_search, parent, false);
holder = new ViewHolder();
holder.image = (ImageView) convertView.findViewById(R.id.iv_thumbnail);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
//Picassoはこの1行で実装できます。
Picasso.with(context).load(thumbnailurl).transform(new CropTransformer()).placeholder(R.drawable.thumback).error(android.R.drawable.ic_dialog_alert).into(holder.image);
return convertView;
}
private class CropTransformer implements Transformation{
@Override
public Bitmap transform(Bitmap source){
int width = source.getWidth();
int height = source.getHeight();
//width, height = (480, 360)
int x = 0;
//y座標の始点をセットする。
int y = (height-width*9/16)/2;
//幅をそのままに、高さを計算する。
height = width*9/16;
Bitmap result = Bitmap.createBitmap(source, x, y, width, height);
if(result!=source){
source.recycle();
}
return result;
}
@Override
public String key(){
return "Custom()";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment