Making a Standard Request

volley 默认支持三种类型的request,stringQueue,ImageRequest,JsonObjectRequest。
volley提供了三种类来request image。分别是ImageRequest,Imageloader,NetworkImageView
下例是使用ImageRequest的例子.
ImageView mImageView;
String url = "http://i.imgur.com/7spzG.png";
mImageView = (ImageView) findViewById(R.id.myImage);
...


// Retrieves an image specified by the URL, displays it in the UI.
ImageRequest request = new ImageRequest(url,
    new Response.Listener<Bitmap>() {
        @Override
        public void onResponse(Bitmap bitmap) {
            mImageView.setImageBitmap(bitmap);
        }
    }, 0, 0, null,
    new Response.ErrorListener() {
        public void onErrorResponse(VolleyError error) {
            mImageView.setImageResource(R.drawable.image_load_error);
        }
    });
// Access the RequestQueue through your singleton class.
MySingleton.getInstance(this).addToRequestQueue(request);


可以使用ImageLoader 和 NetworkImageview 来更有效的获取图片,例如为listview加载图片.
这样必须在xml中声明NetworkImageView
<com.android.volley.toolbox.NetworkImageView
        android:id="@+id/networkImageView"
        android:layout_width="150dp"
        android:layout_height="170dp"
        android:layout_centerHorizontal="true" />


这样就可以在code中按下面的方式使用ImageLoader
ImageLoader mImageLoader;
ImageView mImageView;
// The URL for the image that is being loaded.
private static final String IMAGE_URL =
    "http://developer.android.com/images/training/system-ui.png";
...
mImageView = (ImageView) findViewById(R.id.regularImageView);


// Get the ImageLoader through your singleton class.
mImageLoader = MySingleton.getInstance(this).getImageLoader();
mImageLoader.get(IMAGE_URL, ImageLoader.getImageListener(mImageView,
         R.drawable.def_image, R.drawable.err_image));


也可以使用NetworkImageView
ImageLoader mImageLoader;
NetworkImageView mNetworkImageView;
private static final String IMAGE_URL =
    "http://developer.android.com/images/training/system-ui.png";
...


// Get the NetworkImageView that will display the image.
mNetworkImageView = (NetworkImageView) findViewById(R.id.networkImageView);


// Get the ImageLoader through your singleton class.
mImageLoader = MySingleton.getInstance(this).getImageLoader();


// Set the URL of the image that should be loaded into this view, and
// specify the ImageLoader that will be used to make the request.
mNetworkImageView.setImageUrl(IMAGE_URL, mImageLoader);


除了我们前面讲的DiskBasedCache 外,还可以使用LRU bitmap cache
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
import android.util.DisplayMetrics;
import com.android.volley.toolbox.ImageLoader.ImageCache;


public class LruBitmapCache extends LruCache<String, Bitmap>
        implements ImageCache {


    public LruBitmapCache(int maxSize) {
        super(maxSize);
    }


    public LruBitmapCache(Context ctx) {
        this(getCacheSize(ctx));
    }


    @Override
    protected int sizeOf(String key, Bitmap value) {
        return value.getRowBytes() * value.getHeight();
    }


    @Override
    public Bitmap getBitmap(String url) {
        return get(url);
    }


    @Override
    public void putBitmap(String url, Bitmap bitmap) {
        put(url, bitmap);
    }


    // Returns a cache size equal to approximately three screens worth of images.
    public static int getCacheSize(Context ctx) {
        final DisplayMetrics displayMetrics = ctx.getResources().
                getDisplayMetrics();
        final int screenWidth = displayMetrics.widthPixels;
        final int screenHeight = displayMetrics.heightPixels;
        // 4 bytes per pixel
        final int screenBytes = screenWidth * screenHeight * 4;


        return screenBytes * 3;
    }
}


下面是这个cache使用的情况
RequestQueue mRequestQueue; // assume this exists.
ImageLoader mImageLoader = new ImageLoader(mRequestQueue, new LruBitmapCache(
            LruBitmapCache.getCacheSize()));




volley 为JSON request 提供了两个class,JsonArrayRequest 和JsonObjectRequest
下面是使用JsonObjectRequest 的例子
TextView mTxtDisplay;
ImageView mImageView;
mTxtDisplay = (TextView) findViewById(R.id.txtDisplay);
String url = "http://my-json-feed";


JsonObjectRequest jsObjRequest = new JsonObjectRequest
        (Request.Method.GET, url, null, new Response.Listener<JSONObject>() {


    @Override
    public void onResponse(JSONObject response) {
        mTxtDisplay.setText("Response: " + response.toString());
    }
}, new Response.ErrorListener() {


    @Override
    public void onErrorResponse(VolleyError error) {
        // TODO Auto-generated method stub


    }
});


// Access the RequestQueue through your singleton class.
MySingleton.getInstance(this).addToRequestQueue(jsObjRequest);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
As an AI language model, I can provide some tips on how to make a public speech. 1. Research and Prepare: Start by researching your topic and gathering all the necessary information that you need to deliver a speech. Plan your speech by creating an outline of the main points you want to make. 2. Know your audience: Consider who your audience is and tailor your speech to their interests and knowledge. This will help you to engage them and keep their attention throughout your speech. 3. Be confident: Confidence is key when making a public speech. Practice your speech beforehand and know your material inside out. This will help you to deliver it with poise and clarity. 4. Use body language: Gestures and body language can help to emphasize your points and make your speech more engaging. Use appropriate hand gestures and facial expressions to convey your message. 5. Use visuals: Visual aids such as PowerPoint presentations or props can help to add interest to your speech and make complex ideas easier to understand. 6. Speak clearly and slowly: Make sure to speak clearly and slowly so that your audience can understand what you are saying. Take deep breaths and pause when necessary to maintain your composure. 7. Connect with your audience: Use anecdotes, humor or personal experiences to connect with your audience and build rapport. Remember that a public speech is an opportunity to share knowledge, inspire and motivate others. With thorough preparation and practice, you can give an impactful speech that leaves a lasting impression on your audience.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值