如何将变量传递给此 运行() 函数?
How can I pass a Variable to this run() function?
我这里说的class来自于Android的Picasso Lib。
我在这个函数中有一个名为 "ImageName" 的变量。
但是我似乎无法弄清楚如何将参数传递给函数以更改该变量名称。我已经尝试扩展目标 class 但我似乎仍然遇到错误,而且我还不够先进,不知道我做错了什么。
private Target target = new Target() {
@Override
public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from)
{
new Thread(new Runnable() {
@Override
public void run() {
File file = new File(
Environment.getExternalStorageDirectory().getPath()
+ ImageName + ."jpg");//+ "/saved.jpg");
try {
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG,100,ostream);
ostream.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {}
};
//我同时想到了什么...
public interface TargetImageName extends Target
{
public void TargetImageName(String ImageName);
@Override
void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from);
@Override
void onBitmapFailed(Drawable errorDrawable);
@Override
void onPrepareLoad(Drawable placeHolderDrawable);
}
private TargetImageName target2 = new TargetImageName() {
String name;
@Override
public void TargetImageName(String ImageName) {
name = ImageName;
}
@Override
public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
new Thread(new Runnable() {
@Override
public void run() {
File file = new File(
Environment.getExternalStorageDirectory().getPath()
+ name);//+ "/saved.jpg");
try {
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG,100,ostream);
ostream.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {}
};
您不能将任何内容传递给 run()
,因为它不带任何参数。你应该做的只是扩展 Runnable
并通过构造函数传递你需要的任何东西:
public class MyRunnable implements Runnable {
protected Integer mFoo;
public MyRunnable(Integer foo) {
mFoo = foo;
}
@Override
public void run() {
// access mFoo when you need it
...
}
}
然后
...
new Thread(new MyRunnable(...)) {
...
创建一个实现 Target 的 class 而不是另一个接口。
public class TargetImageName implements Target
{
String name;
public TargetImageName(String ImageName) {
name = ImageName;
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
...}
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
...}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
...}
}
现在创建一个对象并将其作为 Picasso
回调对象传递
Picasso.with(getActivity()).load(<url>).into(new TargetImageName(<parameter>));
我这里说的class来自于Android的Picasso Lib。 我在这个函数中有一个名为 "ImageName" 的变量。 但是我似乎无法弄清楚如何将参数传递给函数以更改该变量名称。我已经尝试扩展目标 class 但我似乎仍然遇到错误,而且我还不够先进,不知道我做错了什么。
private Target target = new Target() {
@Override
public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from)
{
new Thread(new Runnable() {
@Override
public void run() {
File file = new File(
Environment.getExternalStorageDirectory().getPath()
+ ImageName + ."jpg");//+ "/saved.jpg");
try {
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG,100,ostream);
ostream.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {}
};
//我同时想到了什么...
public interface TargetImageName extends Target
{
public void TargetImageName(String ImageName);
@Override
void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from);
@Override
void onBitmapFailed(Drawable errorDrawable);
@Override
void onPrepareLoad(Drawable placeHolderDrawable);
}
private TargetImageName target2 = new TargetImageName() {
String name;
@Override
public void TargetImageName(String ImageName) {
name = ImageName;
}
@Override
public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
new Thread(new Runnable() {
@Override
public void run() {
File file = new File(
Environment.getExternalStorageDirectory().getPath()
+ name);//+ "/saved.jpg");
try {
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG,100,ostream);
ostream.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {}
};
您不能将任何内容传递给 run()
,因为它不带任何参数。你应该做的只是扩展 Runnable
并通过构造函数传递你需要的任何东西:
public class MyRunnable implements Runnable {
protected Integer mFoo;
public MyRunnable(Integer foo) {
mFoo = foo;
}
@Override
public void run() {
// access mFoo when you need it
...
}
}
然后
...
new Thread(new MyRunnable(...)) {
...
创建一个实现 Target 的 class 而不是另一个接口。
public class TargetImageName implements Target
{
String name;
public TargetImageName(String ImageName) {
name = ImageName;
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
...}
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
...}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
...}
}
现在创建一个对象并将其作为 Picasso
回调对象传递
Picasso.with(getActivity()).load(<url>).into(new TargetImageName(<parameter>));