Android 将按钮值从一个活动发送到另一个活动
我创建了两个活动(MainSecond.java),其中包含我的2个按钮和(SegmentBitmapsLoader.java),其中包含我的分段模型。我想做的是:当我点击按钮1时,它会将按钮id发送到(SegmentBitmapsLoader.java)然后初始化model1和button2和model2,问题是:当我试图使用getIntent().getStringExtra()时;在(SegmentBitmapsLoader.java)中,它给了我一个错误,因为这个类没有扩展活动。我应该怎么做 (MainSecond.java) } sectionBitMapsLoader.javaAndroid 将按钮值从一个活动发送到另一个活动,android,Android,我创建了两个活动(MainSecond.java),其中包含我的2个按钮和(SegmentBitmapsLoader.java),其中包含我的分段模型。我想做的是:当我点击按钮1时,它会将按钮id发送到(SegmentBitmapsLoader.java)然后初始化model1和button2和model2,问题是:当我试图使用getIntent().getStringExtra()时;在(SegmentBitmapsLoader.java)中,它给了我一个错误,因为这个类没有扩展活动。我应该怎
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_main);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
BackToMain2(view);
}
});
button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
BackToMain2(view);
}
});
}
public void BackToMain2(View view) {
Intent intent2 = new Intent(MainSecond.this, SegmentBitmapsLoader.class);
intent2.putExtra("name","button1");
intent2.putExtra("name","button2");
startActivity(intent2);
}
public class SegmentBitmapsLoader extends AbsAsyncDataLoader<List<SegmentBitmap>> {
private Uri mImageUri;
public SegmentBitmapsLoader(Context context, Uri imageUri) {
super(context);
mImageUri = imageUri;
}
@Nullable
@Override
public List<SegmentBitmap> loadInBackground() {
final Context context = getContext();
if (context == null) {
return null;
}
final Resources res = context.getResources();
if (res == null) {
return null;
}
if (mImageUri == null) {
return null;
}
final String filePath = FilePickUtils.getPath(context, mImageUri);
Logger.debug("file to mask: %s", filePath);
if (TextUtils.isEmpty(filePath)) {
return null;
}
boolean vertical = checkAndReportDimen(filePath);
final int dw = res.getDimensionPixelSize(
vertical ? R.dimen.image_width_v : R.dimen.image_width_h);
final int dh = res.getDimensionPixelSize(
vertical ? R.dimen.image_height_v : R.dimen.image_height_h);
Logger.debug("display image dimen: [%d x %d]", dw, dh);
Bitmap bitmap = decodeBitmapFromFile(filePath, dw, dh);
if (bitmap == null) {
return null;
}
List<SegmentBitmap> bitmaps = new ArrayList<>();
bitmaps.add(new SegmentBitmap(R.string.label_original, bitmap));//important note
final int w = bitmap.getWidth();
final int h = bitmap.getHeight();
Logger.debug("decoded file dimen: [%d x %d]", w, h);
EventBus.getDefault().post(new ImageDimenEvent(mImageUri, w, h));
float resizeRatio = (float) DeeplabModel.INPUT_SIZE / Math.max(bitmap.getWidth(), bitmap.getHeight());
float resizeRatio2 = (float) DeeplabModel2.INPUT_SIZE / Math.max(bitmap.getWidth(), bitmap.getHeight());
int rw = Math.round(w * resizeRatio);
int rh = Math.round(h * resizeRatio);
int rw2 = Math.round(w * resizeRatio2);
int rh2 = Math.round(h * resizeRatio2);
Logger.debug("resize bitmap: ratio = %f, [%d x %d] -> [%d x %d]",
resizeRatio, w, h, rw, rh);
Logger.debug("resize bitmap: ratio = %f, [%d x %d] -> [%d x %d]",
resizeRatio2, w, h, rw2, rh2);
Bitmap resized = ImageUtils.tfResizeBilinear(bitmap, rw, rh);
Bitmap resized2 = ImageUtils.tfResizeBilinear(bitmap, rw2, rh2);
Bitmap mask = DeeplabModel.segment(resized);
Bitmap mask2 = DeeplabModel2.segment(resized2);
if (mask != null) {
mask = BitmapUtils.scaleBitmap(mask, w, h);
bitmaps.add(new SegmentBitmap(R.string.label_mask, mask));
final Bitmap cropped = cropBitmapWithMask(bitmap, mask);
bitmaps.add(new SegmentBitmap(R.string.label_cropped, cropped));
} else {
bitmaps.add(new SegmentBitmap(R.string.label_mask, (Bitmap) null));
bitmaps.add(new SegmentBitmap(R.string.label_cropped, (Bitmap) null));
}
if(mask2 != null){
mask2 = BitmapUtils.scaleBitmap(mask2, w, h);
bitmaps.add(new SegmentBitmap(R.string.label_mask, mask2));
final Bitmap cropped = cropBitmapWithMask(bitmap, mask2);
bitmaps.add(new SegmentBitmap(R.string.label_cropped, cropped));
}else {
bitmaps.add(new SegmentBitmap(R.string.label_mask, (Bitmap)null));
bitmaps.add(new SegmentBitmap(R.string.label_cropped, (Bitmap)null));
}
return bitmaps;
}
private boolean checkAndReportDimen(String filePath) {
if (TextUtils.isEmpty(filePath)) {
return false;
}
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
final int width = options.outWidth;
final int height = options.outHeight;
Logger.debug("original image dimen: %d x %d", width, height);
EventBus.getDefault().post(new ImageDimenEvent(mImageUri, width, height));
return (height > width);
}
private Bitmap cropBitmapWithMask(Bitmap original, Bitmap mask) {
if (original == null
|| mask == null) {
return null;
}
final int w = original.getWidth();
final int h = original.getHeight();
if (w <= 0 || h <= 0) {
return null;
}
Bitmap cropped = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(cropped);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
canvas.drawBitmap(original, 0, 0, null);
canvas.drawBitmap(mask, 0, 0, paint);
paint.setXfermode(null);
return cropped;
}
public static Bitmap decodeBitmapFromFile(String filePath,
int reqWidth,
int reqHeight) {
if (TextUtils.isEmpty(filePath)) {
return null;
}
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(filePath, options);
}
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) >= reqHeight
&& (halfWidth / inSampleSize) >= reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
}
公共类分段BitMapsLoader扩展了AbsAsyncDataLoader{
私有Uri-mimageri;
公共段BitMapsLoader(上下文上下文,Uri imageUri){
超级(上下文);
mimageri=imageUri;
}
@可空
@凌驾
公共列表加载背景(){
final Context=getContext();
if(上下文==null){
返回null;
}
final Resources res=context.getResources();
如果(res==null){
返回null;
}
if(mimageri==null){
返回null;
}
最终字符串filePath=FilePickUtils.getPath(上下文,mimageri);
debug(“要屏蔽的文件:%s”,文件路径);
if(TextUtils.isEmpty(filePath)){
返回null;
}
布尔垂直=checkAndReportDimen(文件路径);
最终int dw=res.getDimensionPixelSize(
垂直?R.dimen.image\u width\u v:R.dimen.image\u width);
最终int dh=res.getDimensionPixelSize(
垂直?R.dimen.image\u height\u v:R.dimen.image\u height\u h);
Logger.debug(“显示图像尺寸:[%d x%d]”,dw,dh);
位图位图=decodeBitmapFromFile(文件路径,dw,dh);
如果(位图==null){
返回null;
}
列表位图=新的ArrayList();
bitmaps.add(新段位图(R.string.label_original,位图));//重要注意事项
final int w=bitmap.getWidth();
final int h=bitmap.getHeight();
debug(“解码文件尺寸:[%d x%d]”,w,h);
EventBus.getDefault().post(新的ImageDimenEvent(mimeguri,w,h));
float resizeRatio=(float)DeeplabModel.INPUT_SIZE/Math.max(bitmap.getWidth(),bitmap.getHeight());
float resizeRatio2=(float)DeeplabModel2.INPUT_SIZE/Math.max(bitmap.getWidth(),bitmap.getHeight());
int rw=数学圆整(w*resizeRatio);
int rh=数学圆整(h*resizeRatio);
int rw2=数学圆(w*resizeRatio2);
int rh2=数学圆(h*resizeRatio2);
Logger.debug(“调整位图大小:比率=%f,[%d x%d]->[%d x%d]”,
resizeRatio,w,h,rw,rh);
Logger.debug(“调整位图大小:比率=%f,[%d x%d]->[%d x%d]”,
resizeRatio2,w,h,rw2,rh2);
位图大小=ImageUtils.tfresizeblinear(位图、rw、rh);
Bitmap resized2=ImageUtils.tfresizeblinear(位图,rw2,rh2);
位图掩码=DeeplabModel.segment(已调整大小);
位图mask2=DeeplabModel2.segment(调整大小为2);
if(掩码!=null){
mask=BitmapUtils.scaleBitmap(mask,w,h);
添加(新的分段位图(R.string.label_mask,mask));
最终裁剪的位图=cropBitmapWithMask(位图,mask);
添加(新的分段位图(R.string.label_裁剪,裁剪));
}否则{
添加(新段位图(R.string.label_mask,(位图)null));
添加(新段位图(R.string.label_裁剪,(位图)null));
}
if(mask2!=null){
mask2=位图比例图(mask2,w,h);
添加(新的分段位图(R.string.label_mask,mask2));
最终裁剪的位图=cropBitmapWithMask(位图,mask2);
添加(新的分段位图(R.string.label_裁剪,裁剪));
}否则{
添加(新段位图(R.string.label_mask,(位图)null));
添加(新段位图(R.string.label_裁剪,(位图)null));
}
返回位图;
}
专用布尔检查和报告维度(字符串文件路径){
if(TextUtils.isEmpty(filePath)){
返回false;
}
//使用INJUSTDECBOUNDS首次解码=true检查尺寸
final BitmapFactory.Options=new BitmapFactory.Options();
options.inJustDecodeBounds=true;
解码文件(文件路径,选项);
最终整数宽度=options.outWidth;
最终内部高度=options.outHeight;
Logger.debug(“原始图像尺寸:%d x%d”,宽度,高度);
EventBus.getDefault().post(新的ImageDimenEvent(mimeguri,width,height));
返回(高度>宽度);
}
私有位图cropBitmapWithMask(位图原始,位图掩码){
如果(原始==null)
||掩码==null){
返回null;
}
final int w=original.getWidth();
final int h=original.getHeight();
如果(宽度){
最终int半高=高度/2;
最终整数半宽度=宽度/2;
//计算最大的inSampleSize值,该值为2的幂次方,并同时保持这两个值
//高度和宽度大于请求的高度和宽度。
而((半高/采样)>=reqHeight
&&(半宽/采样尺寸)>=reqWidth){
inSampleSize*=2;
}
}
返回样本大小;
}
}
您没有发送按钮id,如果您想发送,您必须将方法更改为:
private void backToMain2(int button_id) {
Intent intent2 = new Intent(MainSecond.this, SegmentBitmapsLoader.class);
intent2.putExtra("button_id",button_id);
startActivity(intent2);
}
当您单击按钮时,请执行以下操作:
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
backToMain2(R.id.button1);
}
});
但我现在知道你为什么要做startActivity()
,sectionBitmapsLoader
这不是活动和startActivity()
,正如文档所说:
开展新活动
SegmentBitmapsLoader segmentBitmapsLoader = new SegmentBitmapsLoader(this,YOUR_URI,YOUR_BUTTON_ID);