#java #android-studio
#java #android-studio
Вопрос:
У меня такое чувство, что это может быть вопрос новичка, поэтому, пожалуйста, потерпите меня, если это так.
Мой код выполняется нормально, но в эмуляторе ничего не отображается. Я думал, что создал программный вид, используя приведенный ниже код, но, похоже, он не генерируется.
package com.example.trimmer_test;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class Trimmer extends Activity {
public Bitmap img1;
public static Bitmap TrimBitmap(Bitmap bmp) {
int imgHeight = bmp.getHeight();
int imgWidth = bmp.getWidth();
//TRIM WIDTH - LEFT
int startWidth = 0;
for (int x = 0; x < imgWidth; x ) {
if (startWidth == 0) {
for (int y = 0; y < imgHeight; y ) {
if (bmp.getPixel(x, y) != Color.TRANSPARENT) {
startWidth = x;
break;
}
}
} else break;
}
//TRIM WIDTH - RIGHT
int endWidth = 0;
for (int x = imgWidth - 1; x >= 0; x--) {
if (endWidth == 0) {
for (int y = 0; y < imgHeight; y ) {
if (bmp.getPixel(x, y) != Color.TRANSPARENT) {
endWidth = x;
break;
}
}
} else break;
}
//TRIM HEIGHT - TOP
int startHeight = 0;
for (int y = 0; y < imgHeight; y ) {
if (startHeight == 0) {
for (int x = 0; x < imgWidth; x ) {
if (bmp.getPixel(x, y) != Color.TRANSPARENT) {
startHeight = y;
break;
}
}
} else break;
}
//TRIM HEIGHT - BOTTOM
int endHeight = 0;
for (int y = imgHeight - 1; y >= 0; y--) {
if (endHeight == 0) {
for (int x = 0; x < imgWidth; x ) {
if (bmp.getPixel(x, y) != Color.TRANSPARENT) {
endHeight = y;
break;
}
}
} else break;
}
return Bitmap.createBitmap(
bmp,
startWidth,
startHeight,
endWidth - startWidth,
endHeight - startHeight
);
}
private final Resources res;
public Trimmer(final Resources res) {
this.res = res;
}
public void My_Bitmap2() {
Bitmap my_bitmap2 = BitmapFactory.decodeResource(this.res, R.drawable.my_bitmap2);
Bitmap img1 = TrimBitmap(my_bitmap2);
}
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
ll.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
ImageView IV1 = new ImageView(this);
IV1.setImageBitmap(img1);
setContentView(ll);
}}
И файл Manifest XML:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.trimmer_test">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Trimmer">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Представление вообще не отображается при запуске.
Комментарии:
1. вы создаете новый макет из кода действия??
2. ДА. Это проблема?
3. Почему бы вам просто не создать макет с помощью файлов .xml? это намного проще для будущей работы и более читаемо. Подумайте, есть ли у текущей активности тонны работы, CRUD и так далее.
4. Это потому, что я хочу запустить соответствующее растровое изображение с помощью первого метода, а затем использовать его в представлении. Вот почему. Хотя мне приходит в голову, что, возможно, было бы эффективнее просто изменить растровый логотип в автономном режиме, а затем добавить его с помощью обычного XML.
5. Но теоретически, вышеупомянутое должно работать, не так ли?