本文介绍了如何在一个片段的setContentView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
现在我已经得到了这个片段,我想使用的setContentView用,但到目前为止,我无法找到如何。你可以看到我的情况下,在code以下,我不是想夸大的布局,即时通讯试图使用它被称为SampleView的看法。所以,我该怎么办呢?在此先感谢
公共类largeImageScroller扩展SherlockFragment {
//物理显示宽度和高度。
私有静态诠释displayWidth = 0;
私有静态诠释displayHeight = 0;
/ **第一次创建活动时调用。 * /
公共查看onCreateView(LayoutInflater充气,ViewGroup中组,包保存){
getActivity();
// displayWidth和displayHeight会根据屏幕改变
// 方向。要动态地获得这些,我们应该勾onSizeChanged()。
//这个简单的例子只使用风景模式,所以它的确定,让他们
//一旦启动,并在整个使用这些值。
显示显示=((窗口管理器)
。getActivity()getSystemService(Context.WINDOW_SERVICE))getDefaultDisplay()。
displayWidth = display.getWidth();
displayHeight = display.getHeight();
// SampleView构造必须最后构造,因为它需要的
// displayWidth和displayHeight我们刚刚得到。
的setContentView(新SampleView(本));
}
私有静态类SampleView扩展视图{
私有静态位图bmLargeImage; //位图大到足以被滚动
私有静态矩形displayRect = NULL; // RECT我们显示给
私人矩形scrollRect的= NULL; // RECT我们滚动在我们的位图
私人诠释scrollRectX = 0; //滚动矩形的电流左侧位置
私人诠释scrollRectY = 0; //滚动矩形的目前最顶级的位置
私人浮动scrollByX = 0; // X量由滚动
私人浮动scrollByY = 0; //ÿ量滚动
私人浮动STARTX = 0; //轨道x在1 ACTION_MOVE到下
私人浮动startY = 0; //轨道Ÿ从一个ACTION_MOVE下
公共SampleView(上下文的背景下){
超(上下文);
//为我们的主画布绘制目标正确。它永远不会改变。
displayRect =新的Rect(0,0,displayWidth,displayHeight);
//滚动矩形:这将用于滚动围绕着过
//位图存储器中。如上初始化。
scrollRect的=新的Rect(0,0,displayWidth,displayHeight);
//加载一个大的位图到内存的一个屏幕外区域。
bmLargeImage = BitmapFactory.de codeResource(getResources()
R.drawable.ground_floor_b);
}
@覆盖
公共布尔的onTouchEvent(MotionEvent事件){
开关(event.getAction()){
案例MotionEvent.ACTION_DOWN:
//还记得我们最初按下事件的位置。
STARTX = event.getRawX();
startY = event.getRawY();
打破;
案例MotionEvent.ACTION_MOVE:
浮X = event.getRawX();
浮动Y = event.getRawY();
//计算此举更新。这将发生很多次
//一个单一的运动姿势的过程中。
scrollByX = X - STARTX; //移动中会更新X增量
scrollByY = Y - startY; //此举更新ÿ增量
STARTX = X; //重新设置初始值到最新
startY = Y;
无效(); //强制重绘
打破;
}
返回true; //这个事件做了消耗它
}
@覆盖
保护无效的OnDraw(帆布油画){
//我们的移动更新计算ACTION_MOVE在相反的方向
从我们要如何移动滚动RECT //。认为这是拖到
//左边是一样滑动滚动矩形右侧。
INT newScrollRectX = scrollRectX - (INT)scrollByX;
INT newScrollRectY = scrollRectY - (INT)scrollByY;
//不要滚动出位图的左侧或右侧边缘。
如果(newScrollRectX℃,)
newScrollRectX = 0;
否则,如果(newScrollRectX>(bmLargeImage.getWidth() - displayWidth))
newScrollRectX =(bmLargeImage.getWidth() - displayWidth);
//不要滚动出位图的顶部或底部边缘。
如果(newScrollRectY℃,)
newScrollRectY = 0;
否则,如果(newScrollRectY>(bmLargeImage.getHeight() - displayHeight))
newScrollRectY =(bmLargeImage.getHeight() - displayHeight);
//我们有更新的滚动矩形坐标,将它们和借鉴。
scrollRect.set(newScrollRectX,newScrollRectY,
newScrollRectX + displayWidth,newScrollRectY + displayHeight);
涂料粉刷=新的油漆();
canvas.drawBitmap(bmLargeImage,scrollRect的,displayRect,油漆);
//重置当前的滚动坐标,以反映最新的更新,
//所以我们可以重复这个更新过程。
scrollRectX = newScrollRectX;
scrollRectY = newScrollRectY;
}
}
}
解决方案
您不叫的setContentView
的片段,其实你需要返回一个查看
从 onCreateView
。
尝试更换:
的setContentView(新SampleView(本));
通过这样的:
返回新SampleView(本);
Now I've got this fragment which i want to use setContentView with but so far i cant find how. You can see my case in the code below, im not trying to inflate a layout, im trying to use it with the view called SampleView. So how can I do that?Thanks in advance
public class largeImageScroller extends SherlockFragment {
// Physical display width and height.
private static int displayWidth = 0;
private static int displayHeight = 0;
/** Called when the activity is first created. */
public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle saved) {
getActivity();
// displayWidth and displayHeight will change depending on screen
// orientation. To get these dynamically, we should hook onSizeChanged().
// This simple example uses only landscape mode, so it's ok to get them
// once on startup and use those values throughout.
Display display = ((WindowManager)
getActivity().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
displayWidth = display.getWidth();
displayHeight = display.getHeight();
// SampleView constructor must be constructed last as it needs the
// displayWidth and displayHeight we just got.
setContentView(new SampleView(this));
}
private static class SampleView extends View {
private static Bitmap bmLargeImage; //bitmap large enough to be scrolled
private static Rect displayRect = null; //rect we display to
private Rect scrollRect = null; //rect we scroll over our bitmap with
private int scrollRectX = 0; //current left location of scroll rect
private int scrollRectY = 0; //current top location of scroll rect
private float scrollByX = 0; //x amount to scroll by
private float scrollByY = 0; //y amount to scroll by
private float startX = 0; //track x from one ACTION_MOVE to the next
private float startY = 0; //track y from one ACTION_MOVE to the next
public SampleView(Context context) {
super(context);
// Destination rect for our main canvas draw. It never changes.
displayRect = new Rect(0, 0, displayWidth, displayHeight);
// Scroll rect: this will be used to 'scroll around' over the
// bitmap in memory. Initialize as above.
scrollRect = new Rect(0, 0, displayWidth, displayHeight);
// Load a large bitmap into an offscreen area of memory.
bmLargeImage = BitmapFactory.decodeResource(getResources(),
R.drawable.ground_floor_b);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// Remember our initial down event location.
startX = event.getRawX();
startY = event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
float x = event.getRawX();
float y = event.getRawY();
// Calculate move update. This will happen many times
// during the course of a single movement gesture.
scrollByX = x - startX; //move update x increment
scrollByY = y - startY; //move update y increment
startX = x; //reset initial values to latest
startY = y;
invalidate(); //force a redraw
break;
}
return true; //done with this event so consume it
}
@Override
protected void onDraw(Canvas canvas) {
// Our move updates are calculated in ACTION_MOVE in the opposite direction
// from how we want to move the scroll rect. Think of this as dragging to
// the left being the same as sliding the scroll rect to the right.
int newScrollRectX = scrollRectX - (int)scrollByX;
int newScrollRectY = scrollRectY - (int)scrollByY;
// Don't scroll off the left or right edges of the bitmap.
if (newScrollRectX < 0)
newScrollRectX = 0;
else if (newScrollRectX > (bmLargeImage.getWidth() - displayWidth))
newScrollRectX = (bmLargeImage.getWidth() - displayWidth);
// Don't scroll off the top or bottom edges of the bitmap.
if (newScrollRectY < 0)
newScrollRectY = 0;
else if (newScrollRectY > (bmLargeImage.getHeight() - displayHeight))
newScrollRectY = (bmLargeImage.getHeight() - displayHeight);
// We have our updated scroll rect coordinates, set them and draw.
scrollRect.set(newScrollRectX, newScrollRectY,
newScrollRectX + displayWidth, newScrollRectY + displayHeight);
Paint paint = new Paint();
canvas.drawBitmap(bmLargeImage, scrollRect, displayRect, paint);
// Reset current scroll coordinates to reflect the latest updates,
// so we can repeat this update process.
scrollRectX = newScrollRectX;
scrollRectY = newScrollRectY;
}
}
}
解决方案
You dont call setContentView
in fragments, in fact you need to return a View
from onCreateView
.
Try replacing:
setContentView(new SampleView(this));
With this:
return new SampleView(this);
这篇关于如何在一个片段的setContentView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!