When you can use ImageView. setImageBitmap (bitmap). What else besides canvas to dynamically change the image in ImageView
Here I have an imageview, if in the onCreate method I assign ImageView. setImageBitmap(bitmap), then everything is displayed normally. And if in some other method, after the onCreate method has worked, I get the error Reference to a null object. I.e., the setImageBitmap method is not suitable for dynamically changing the image in ImageView during the application operation? And if I want to get different images in one ImageView, what should I do?
Убрал всё лишнее в коде. Как видите внизу есть Set метод . И если с другого класса пытаюсь присвоить изображение получаю ошибку. А если сразу в OnCreate присваивать то изображение появляется. Но мне нужно в дальнейшем часто менять этот ImageView
public class Menu extends AppCompatActivity {
ImageView imageView1,imageView2,imageView3,imageView4,imageView5,imageView6,imageView7;
Bitmap[] myArray = new Bitmap[11];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
mainGamePanel = (MainGamePanel) findViewById(R.id.surface);
bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.one);
imageView1 = (ImageView) this.findViewById(R.id.imageView1);
imageView2 = (ImageView) this.findViewById(R.id.imageView2);
imageView3 = (ImageView) this.findViewById(R.id.imageView3);
imageView4 = (ImageView) this.findViewById(R.id.imageView4);
imageView5 = (ImageView) this.findViewById(R.id.imageView5);
imageView6 = (ImageView) this.findViewById(R.id.imageView6);
imageView7 = (ImageView) this.findViewById(R.id.imageView7);
myArray[0] = BitmapFactory.decodeResource(getResources(), R.mipmap.zero0);
myArray[1] = BitmapFactory.decodeResource(getResources(), R.mipmap.one);
myArray[2] = BitmapFactory.decodeResource(getResources(), R.mipmap.two);
myArray[3] = BitmapFactory.decodeResource(getResources(), R.mipmap.three);
myArray[4] = BitmapFactory.decodeResource(getResources(), R.mipmap.four);
myArray[5] = BitmapFactory.decodeResource(getResources(), R.mipmap.five);
myArray[6] = BitmapFactory.decodeResource(getResources(), R.mipmap.six);
myArray[7] = BitmapFactory.decodeResource(getResources(), R.mipmap.seven);
myArray[8] = BitmapFactory.decodeResource(getResources(), R.mipmap.eight);
myArray[9] = BitmapFactory.decodeResource(getResources(), R.mipmap.nine);
myArray[10] = BitmapFactory.decodeResource(getResources(), R.mipmap.point);
}
public void SetTime (int Seconds1){
imageView1.setImageBitmap(myArray[Seconds1]);
}
}
Set I call the method from the class that is declared in this markup
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_menu"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="example.igeniy.Menu">
<view
class="example.igeniy.MainGamePanel"
android:id="@+id/surface"
android:layout_width="match_parent"
android:layout_height="match_parent">
</view>
<android.support.v7.widget.LinearLayoutCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:id="@+id/imageView1"
android:layout_gravity="center_horizontal|top"
android:layout_height="wrap_content" />
</android.support.v7.widget.LinearLayoutCompat>
</FrameLayout>
I'm trying to call the {[4] constructor at the end of the method]}
public class MainGamePanel extends SurfaceView implements SurfaceHolder.Callback {
private static final String TAG = MainThread.class.getSimpleName();
private MainThread thread;
private Chronometer Treadchronometer;
private mainCharacter Character, Character1, Character2, Character3, Character4;
private Time time;
Menu menu = new Menu();
Bitmap[] myArray = new Bitmap[11];
SurfaceView mSurfaceView;
SurfaceHolder mSurfaceHolder;
public MainGamePanel(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
// Для устройств до Android 5
createOldSoundPool();
} else {
// Для новых устройств
createNewSoundPool();
}
mAssetManager = getContext().getAssets();
// получим идентификаторы
sExplosion = loadSound("explosion.wav");
getHolder().addCallback(this);
touchpad = BitmapFactory.decodeResource(getResources(), R.mipmap.touchpad);
myArray[0] = BitmapFactory.decodeResource(getResources(), R.mipmap.zero0);
myArray[1] = BitmapFactory.decodeResource(getResources(), R.mipmap.one);
myArray[2] = BitmapFactory.decodeResource(getResources(), R.mipmap.two);
myArray[3] = BitmapFactory.decodeResource(getResources(), R.mipmap.three);
myArray[4] = BitmapFactory.decodeResource(getResources(), R.mipmap.four);
myArray[5] = BitmapFactory.decodeResource(getResources(), R.mipmap.five);
myArray[6] = BitmapFactory.decodeResource(getResources(), R.mipmap.six);
myArray[7] = BitmapFactory.decodeResource(getResources(), R.mipmap.seven);
myArray[8] = BitmapFactory.decodeResource(getResources(), R.mipmap.eight);
myArray[9] = BitmapFactory.decodeResource(getResources(), R.mipmap.nine);
myArray[10] = BitmapFactory.decodeResource(getResources(), R.mipmap.point);
NewGame();
setFocusable(true);
menu.SetTime(5); // ВОТ ТУТ ПЫТАЮСЬ ВЫЗВАТЬ Сеттер
}
public void NewGame (){
Character = new mainCharacter(BitmapFactory.decodeResource(getResources(), face_box), 10, 10, 100, 100);
Character1 = new mainCharacter(BitmapFactory.decodeResource(getResources(), R.mipmap.face_circle_tiled), 50, 300, 100, 100);
Character2 = new mainCharacter(BitmapFactory.decodeResource(getResources(), R.mipmap.face_circle_tiled), 400, 200, 100, 100);
Character3 = new mainCharacter(BitmapFactory.decodeResource(getResources(), R.mipmap.face_circle_tiled), 300, 30, 100, 100);
Character4 = new mainCharacter(BitmapFactory.decodeResource(getResources(), R.mipmap.face_circle_tiled), 34, 30, 100, 100);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Log.d(TAG,"surfaceChanged");
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
thread = new MainThread(getHolder(), this);
thread.setRunning(true);
thread.start();
Treadchronometer = new Chronometer();
Treadchronometer.setRunning(true);
Treadchronometer.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
destroy();
}
@Override
public boolean onTouchEvent(MotionEvent event) {}
@Override
protected void onDraw(Canvas canvas) {
// Заливаем canvas белым цветом
canvas.drawColor(Color.rgb(190, 223, 220)); // PorterDuff.Mode.CLEAR - используется, чтобы ничего не отображать подробнее в PorterDuff режимы
// Вызываем метод, который выводит рисунок главного персонажа
canvas.drawBitmap(touchpad, 0, menu.getHeight() * 0.7F, null);
Character1.draw(canvas);
Character2.draw(canvas);
Character3.draw(canvas);
Character4.draw(canvas);
Character.draw(canvas);
}
}
0
1 answers
Here, for example, is the working code. Everything changes well.
public class MainActivity extends AppCompatActivity {
Bitmap bitmap1;
Bitmap bitmap2;
ImageView view;
boolean whatBitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view = (ImageView) findViewById(R.id.imageView);
bitmap1 = BitmapFactory.decodeResource(getResources(), R.drawable.test);
bitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.testtwo);
}
public void click(View v)
{
if (whatBitmap) {
view.setImageBitmap(bitmap1);
whatBitmap = false;
}
else {
view.setImageBitmap(bitmap2);
whatBitmap = true;
}
}
}
2
Author: Кирилл Малышев, 2017-01-29 11:37:49