ImageView 有一个视图 ID 但没有参数
ImageView has an View ID but no Parameters
所以首先我知道有很多关于 ImageViews 的问题和答案,但我已经研究过这个网站并没有真正找到答案。由于我不是出生在英国国家,我想提前为我惊人的英语能力道歉。
我的问题:我有一个 activity 应该 Show/Draw 一个给定的图像,我试图用一个片段来实现这一点,因为我希望每次我绘制不同的图像打开片段。此外,当 Fragment 位于前台时,我不想要任何 UserInput。稍微短一点:图像弹出大约一秒钟然后再次消失,用户无法控制图像显示的内容、时间、方式和位置。当我试图在 onCreate 的 Fragment 中获取 ImageView(也尝试过 onResume、onActivityCreated...)然后创建一个位图并在其中绘制我的图像时,我遇到了这个问题。
Logcat 没有给我任何错误,程序只是在 "creating" 位图时死掉。问题是我以某种方式获得了 ImageView,但它没有高度、宽度或除了 ID 之外的任何东西。
这是代码:
调用片段的Activity:
package com.example.sehtestapp;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
public class AnswerViewer extends ActionBarActivity
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.answer_viewer);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
int id = item.getItemId();
if (id == R.id.action_settings) {
//This calls the Fragment
ViewerFragment viewerFragment = new ViewerFragment();
Bundle args = new Bundle();
args.putInt("index", 200);
viewerFragment.setArguments(args);
getSupportFragmentManager().beginTransaction().add(R.id.answer_layout, viewerFragment).commit();
return true;
}
return super.onOptionsItemSelected(item);
}
}
然后我们有片段本身:
package com.example.sehtestapp;
import java.util.Random;
import android.support.v4.app.Fragment;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
public class ViewerFragment extends Fragment{
int size;
View view;
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
Bundle args = getArguments();
size = args.getInt("index");
/**
* Inflate the layout for this fragment
*/
view = inflater.inflate(R.layout.viewer_fragment, container, false);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstances){
Random r = new Random();
short direction = (short) r.nextInt(9);
ImageView iv = (ImageView) view.findViewById(R.id.SehtestViewer);
Bitmap bitmap = Bitmap.createBitmap(iv.getWidth(), iv.getHeight(), Bitmap.Config.ARGB_8888);
//this line above breakes the Programm. the imageView gets and ID but no other params.
//here is just alot of drawing to which the Programm never gets
}
}
这里是viewer_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background = "#FFBB00"
android:id ="@+id/viewer_layout">
<ImageView
android:id="@+id/SehtestViewer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/abc_textfield_searchview_right_holo_dark"
android:contentDescription="@string/viewfield_of_sehtestring"/>
感谢所有提前回复和阅读,我希望得到一些快速回复:D
视图的宽度和高度在实际渲染之前不会设置(因此在 onCreate 中,您的宽度和高度将始终为零)
您应该使用 ViewTreeObserver:
ViewTreeObserver vtObserver = iv.getViewTreeObserver();
vtObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener()
{
@Override
public void onGlobalLayout()
{
iv.getViewTreeObserver().removeGlobalOnLayoutListener(this);
int width = iv.getMeasuredWidth();
int height = iv.getMeasuredHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
}
});
在answer_viewer.xml中添加布局如下
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background = "#FFBB00"
android:id ="@+id/fragment_container">
在AnswerViewer.java文件中,在public boolean onOptionsItemSelected(MenuItem item)函数中,
替换为这条语句。
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, viewerFragment).commit();
移动此语句
Bundle args = getArguments();
到onActivityCreated()
希望对您有所帮助。
所以首先我知道有很多关于 ImageViews 的问题和答案,但我已经研究过这个网站并没有真正找到答案。由于我不是出生在英国国家,我想提前为我惊人的英语能力道歉。
我的问题:我有一个 activity 应该 Show/Draw 一个给定的图像,我试图用一个片段来实现这一点,因为我希望每次我绘制不同的图像打开片段。此外,当 Fragment 位于前台时,我不想要任何 UserInput。稍微短一点:图像弹出大约一秒钟然后再次消失,用户无法控制图像显示的内容、时间、方式和位置。当我试图在 onCreate 的 Fragment 中获取 ImageView(也尝试过 onResume、onActivityCreated...)然后创建一个位图并在其中绘制我的图像时,我遇到了这个问题。
Logcat 没有给我任何错误,程序只是在 "creating" 位图时死掉。问题是我以某种方式获得了 ImageView,但它没有高度、宽度或除了 ID 之外的任何东西。
这是代码:
调用片段的Activity:
package com.example.sehtestapp;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
public class AnswerViewer extends ActionBarActivity
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.answer_viewer);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
int id = item.getItemId();
if (id == R.id.action_settings) {
//This calls the Fragment
ViewerFragment viewerFragment = new ViewerFragment();
Bundle args = new Bundle();
args.putInt("index", 200);
viewerFragment.setArguments(args);
getSupportFragmentManager().beginTransaction().add(R.id.answer_layout, viewerFragment).commit();
return true;
}
return super.onOptionsItemSelected(item);
}
}
然后我们有片段本身:
package com.example.sehtestapp;
import java.util.Random;
import android.support.v4.app.Fragment;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
public class ViewerFragment extends Fragment{
int size;
View view;
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
Bundle args = getArguments();
size = args.getInt("index");
/**
* Inflate the layout for this fragment
*/
view = inflater.inflate(R.layout.viewer_fragment, container, false);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstances){
Random r = new Random();
short direction = (short) r.nextInt(9);
ImageView iv = (ImageView) view.findViewById(R.id.SehtestViewer);
Bitmap bitmap = Bitmap.createBitmap(iv.getWidth(), iv.getHeight(), Bitmap.Config.ARGB_8888);
//this line above breakes the Programm. the imageView gets and ID but no other params.
//here is just alot of drawing to which the Programm never gets
}
}
这里是viewer_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background = "#FFBB00"
android:id ="@+id/viewer_layout">
<ImageView
android:id="@+id/SehtestViewer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/abc_textfield_searchview_right_holo_dark"
android:contentDescription="@string/viewfield_of_sehtestring"/>
感谢所有提前回复和阅读,我希望得到一些快速回复:D
视图的宽度和高度在实际渲染之前不会设置(因此在 onCreate 中,您的宽度和高度将始终为零)
您应该使用 ViewTreeObserver:
ViewTreeObserver vtObserver = iv.getViewTreeObserver();
vtObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener()
{
@Override
public void onGlobalLayout()
{
iv.getViewTreeObserver().removeGlobalOnLayoutListener(this);
int width = iv.getMeasuredWidth();
int height = iv.getMeasuredHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
}
});
在answer_viewer.xml中添加布局如下
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background = "#FFBB00"
android:id ="@+id/fragment_container">
在AnswerViewer.java文件中,在public boolean onOptionsItemSelected(MenuItem item)函数中, 替换为这条语句。
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, viewerFragment).commit();
移动此语句
Bundle args = getArguments();
到onActivityCreated()
希望对您有所帮助。