如何将值作为参数传递给视图时设置值

时间:2014-12-08 12:00:38

标签: android android-layout android-activity

Picasso.with(Settings_Activity.this).load(R.drawable.nopic).placeholder(getResources().getDrawable(R.drawable.nopic))
		.resize(300, 300).into(profilePic);

这是与Picasso库一起使用的代码,因为我们将图像视图作为参数传递,然后在该视图中加载特定图像,同样我想传递线性布局作为参数然后会有背景将被加载到其中的图像,我该怎么做,需要帮助

1 个答案:

答案 0 :(得分:3)

首先你需要设置Target .eg。

Picasso.with(MainActivity.this).load("http://url").into(target);

这是目标:

 private Target target = new Target() {
    @Override
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {

        if (android.os.Build.VERSION.SDK_INT >= 16) {
            setBackgroundV16Plus(content, bitmap);
        } else {
            setBackgroundV16Minus(content, bitmap);
        }
    }

    @Override
    public void onBitmapFailed(Drawable errorDrawable) {

    }

    @Override
    public void onPrepareLoad(Drawable placeHolderDrawable) {

    }
};

以下是其他两种方法:

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void setBackgroundV16Plus(View view, Bitmap bitmap) {
    view.setBackground(new BitmapDrawable(getResources(), bitmap));

}

@SuppressWarnings("deprecation")
private void setBackgroundV16Minus(View view, Bitmap bitmap) {
    view.setBackgroundDrawable(new BitmapDrawable(bitmap));
}

并且内容是您的视图(线性布局,FrameLayout等)

private View content;

跳跃,它将解决你的问题:)

<<< -------------- UPDATE ------------>>

public class MyActivity extends Activity{

    private Target target = new Target() {
        @Override
        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {

            if (android.os.Build.VERSION.SDK_INT >= 16) {
                setBackgroundV16Plus(content, bitmap);
            } else {
                setBackgroundV16Minus(content, bitmap);
            }
        }

        @Override
        public void onBitmapFailed(Drawable errorDrawable) {

        }

        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {

        }
    };
    private LinearLayout content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layout);
        content=(LinearLayout)findViewById(R.layout.content);
        Picasso.with(MainActivity.this).load("http://url").into(target);

    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void setBackgroundV16Plus(View view, Bitmap bitmap) {
        view.setBackground(new BitmapDrawable(getResources(), bitmap));

    }

    @SuppressWarnings("deprecation")
    private void setBackgroundV16Minus(View view, Bitmap bitmap) {
        view.setBackgroundDrawable(new BitmapDrawable(bitmap));
    }
}