用下载的Bitmap替换Drawable

时间:2016-10-24 15:13:20

标签: android

我试图用下载的位图替换资源drawable。

为实现这一目标,我将活动附加到自定义基本上下文,如下所示:

#include<iostream>
#include<istream>

std::istream & print(std::istream &is) {
    static unsigned cnt = 0;
    ++cnt;
    std::cout << cnt << ((cnt % 10 == 1) ? "st" :
        (cnt % 10 == 2) ? "nd" : "th")
        << " call print" << "\n";

    std::cout<< "is.badbit: " << is.badbit << "\n"
        << "is.failbit: " << is.failbit << "\n"
        << "is.eofbit: " << is.eofbit << "\n"
        << "is.bad(): " << is.bad() << "\n" 
        << "is.fail(): " << is.fail() << "\n"
        << "is.eof(): " << is.eof() << "\n"
        << "is.rdstate(): " << is.rdstate() << "\n";
    return is;
}

using std::cin;
using std::cout;
using std::endl;
using std::vector;

int main()
{
    cout << "sizeof(iostate): " <<sizeof(std::istream::iostate)<<"\n";
    cout << "sizeof(goodbit): " << sizeof(std::istream::goodbit) << "\n";
    cout << typeid(std::istream::goodbit).name();
    cout << endl;

    print(cin);
    cout << endl;

    cin.setstate(std::istream:: badbit);
    print(cin);
    cout << endl;

    cin.clear();
    cin.setstate(std::istream::failbit);
    print(cin);
    cout << endl;

    cin.clear();
    cin.setstate(std::istream::eofbit);
    print(cin);
    cout << endl;


    cin.clear();
    cin.setstate(std::istream::badbit);
    cin.setstate(std::istream::failbit);
    print(cin);
    cout << endl;

    cin.clear();
    cin.setstate(std::istream::badbit);
    cin.setstate(std::istream::eofbit);
    print(cin);
    cout << endl;

    cin.clear();
    cin.setstate(std::istream::failbit);
    cin.setstate(std::istream::eofbit);
    print(cin);
    cout << endl;
}

但是,只有通过sizeof(iostate): 4 sizeof(goodbit): 4 1st call print is.badbit: 4 is.failbit: 2 is.eofbit: 1 is.bad(): 0 is.fail(): 0 is.eof(): 0 is.rdstate(): 0 2nd call print is.badbit: 4 is.failbit: 2 is.eofbit: 1 is.bad(): 1 is.fail(): 1 is.eof(): 0 is.rdstate(): 4 3th call print is.badbit: 4 is.failbit: 2 is.eofbit: 1 is.bad(): 0 is.fail(): 1 is.eof(): 0 is.rdstate(): 2 4th call print is.badbit: 4 is.failbit: 2 is.eofbit: 1 is.bad(): 0 is.fail(): 0 is.eof(): 1 is.rdstate(): 1 5th call print is.badbit: 4 is.failbit: 2 is.eofbit: 1 is.bad(): 1 is.fail(): 1 is.eof(): 0 is.rdstate(): 6 6th call print is.badbit: 4 is.failbit: 2 is.eofbit: 1 is.bad(): 1 is.fail(): 1 is.eof(): 1 is.rdstate(): 5 7th call print is.badbit: 4 is.failbit: 2 is.eofbit: 1 is.bad(): 0 is.fail(): 1 is.eof(): 1 is.rdstate(): 3 引用的图片才会触发public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override protected void attachBaseContext(Context newBase) { super.attachBaseContext(new InterceptingContextWrapper(newBase)); } private class InterceptingContextWrapper extends ContextWrapper { private Resources resources; public InterceptingContextWrapper(Context base) { super(base); resources = new Resources(base.getAssets(), base.getResources().getDisplayMetrics(), base.getResources().getConfiguration()){ @Override public Drawable getDrawable(int id, Theme theme) throws NotFoundException { String resourceName = getResourceName(id); if (id == R.drawable.sample) { Log.i("InterceptionTest", "gotcha"); } Log.i("InterceptionTest", "Getting drawable for resource " + resourceName); return super.getDrawable(id, theme); } }; } @Override public Resources getResources() { return resources; } } } 方法。通过getDrawableapp:srcCompat引用的图片不会通过android:src检索到;然而,一些图像(主要是从android命名空间引用的图像(android:background)出现。(但不是全部)

如果我继承getDrawable而不是@android:drawable/...,那么这也无济于事。

有什么想法吗? 感谢。

0 个答案:

没有答案