我正在为我的应用程序开发网络.所以我决定尝试Square的Retrofit.我看到他们支持简单Callback
@GET("/user/{id}/photo")
void getUserPhoto(@Path("id") int id, Callback<Photo> cb);
Run Code Online (Sandbox Code Playgroud)
和RxJava的 Observable
@GET("/user/{id}/photo")
Observable<Photo> getUserPhoto(@Path("id") int id);
Run Code Online (Sandbox Code Playgroud)
乍一看两者看起来都非常相似,但是当它实现时它变得有趣......
虽然简单的回调实现看起来类似于:
api.getUserPhoto(photoId, new Callback<Photo>() {
@Override
public void onSuccess() {
}
});
Run Code Online (Sandbox Code Playgroud)
这非常简单明了.随着Observable它迅速变得冗长和复杂.
public Observable<Photo> getUserPhoto(final int photoId) {
return Observable.create(new Observable.OnSubscribeFunc<Photo>() {
@Override
public Subscription onSubscribe(Observer<? super Photo> observer) {
try {
observer.onNext(api.getUserPhoto(photoId));
observer.onCompleted();
} catch (Exception e) {
observer.onError(e);
}
return Subscriptions.empty();
}
}).subscribeOn(Schedulers.threadPoolForIO());
}
Run Code Online (Sandbox Code Playgroud)
那不是它.你仍然需要做这样的事情:
Observable.from(photoIdArray)
.mapMany(new Func1<String, Observable<Photo>>() {
@Override
public Observable<Photo> call(Integer s) { …Run Code Online (Sandbox Code Playgroud) 由于某些未知原因,我的应用程序突然无法从Android Studio构建.
我一直在接受
> com.android.ide.common.internal.LoggedErrorException: Failed to run command:
/home/martynas/android-sdk/build-tools/19.1.0/dx --dex --num-threads=4 --output
...
...
...
Error Code:
2
Output:
UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Cannot merge new index 65536 into a non-jumbo instruction!
Run Code Online (Sandbox Code Playgroud)
虽然从命令行成功构建了相同的应用程序.我检查了方法引用计数,它低于可怕的64k.
我现在正在使用AS 0.8.11.
我几天来一直在与这个功能作斗争......
看来,相机忽略了(?)我定义的焦点区域.以下是代码片段:
聚焦:
protected void focusOnTouch(MotionEvent event) {
if (camera != null) {
Rect rect = calculateFocusArea(event.getX(), event.getY());
Parameters parameters = camera.getParameters();
parameters.setFocusMode(Parameters.FOCUS_MODE_AUTO);
parameters.setFocusAreas(Lists.newArrayList(new Camera.Area(rect, 500)));
camera.setParameters(parameters);
camera.autoFocus(this);
}
}
Run Code Online (Sandbox Code Playgroud)
重点区域计算:
private Rect calculateFocusArea(float x, float y) {
int left = clamp(Float.valueOf((x / getSurfaceView().getWidth()) * 2000 - 1000).intValue(), focusAreaSize);
int top = clamp(Float.valueOf((y / getSurfaceView().getHeight()) * 2000 - 1000).intValue(), focusAreaSize);
return new Rect(left, top, left + focusAreaSize, top + focusAreaSize);
}
Run Code Online (Sandbox Code Playgroud)
几个日志事件来自 Camera.AutoFocusCallback#onAutoFocus
Log.d(TAG, String.format("Auto focus success=%s. Focus mode: '%s'. …
我有ActionBar Tabs设置.它由4个标签组成.一切都很好,直到我离开TabbedFragment并返回.
我创建这样的标签:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final ActionBar actionBar = getActionBar();
tabs = Lists.newArrayList();
tabs.add(new TabDefinition<>("Tab 1"));
tabs.add(new TabDefinition<>("Tab 2"));
tabs.add(new TabDefinition<>("Tab 3"));
tabs.add(new TabDefinition<>("Tab 4"));
for (TabDefinition tab : tabs) {
actionBar.addTab(actionBar.newTab()
.setText(tab.text)
.setTag(tab.tag)
.setTabListener(this));
}
}
Run Code Online (Sandbox Code Playgroud)
并初始化适配器,如下所示:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.paging_tab_container, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
viewPager = (ViewPager) view.findViewById(R.id.pager);
viewPager.setAdapter(new FragmentStatePagerAdapter(getFragmentManager()) {
@Override
public Fragment …Run Code Online (Sandbox Code Playgroud) 更新android-support库后,22.2.0项目停止编译.
error: cannot access DialogStyle
class file for android.support.v4.app.DialogFragment$DialogStyle not found
Consult the following stack trace for details.
com.sun.tools.javac.code.Symbol$CompletionFailure: class file for android.support.v4.app.DialogFragment$DialogStyle not found
Run Code Online (Sandbox Code Playgroud)
找不到如何解决这个问题.
以前用过的版本是 22.1.1
使用Kotlin 1.20.20(不重要,旧版本的行为相同)
当布局位于单独的库模块中时,Android Studio在查找和引用视图时没有问题
import kotlinx.android.synthetic.main.view_user_input.*
但是,当我尝试编译应用程序失败与Unresolved reference: view_user_input上:app:compileDebugKotlin.
在库模块中引用视图时,一切正常.
我在这里错过了什么吗?
添加项目结构.所有模块都使用kotlin和kotlin扩展.
build.gradle
app/
build.gradle //main application
library-module-a/
build.gradle //library application
library-module-b/
build.gradle //library application
Run Code Online (Sandbox Code Playgroud)
这是一个示例应用程序https://github.com/mjurkus/KotlinxTest
在KT跟踪器中注册的问题
有没有办法使用Parceler与Kotlin数据类和构造函数进行序列化而不使用@ParcelProperty每个字段的注释?
如果我尝试使用这样的库:
@Parcel
data class Valve @ParcelConstructor constructor(val size: Int)
Run Code Online (Sandbox Code Playgroud)
我得到Error:Parceler: No corresponding property found for constructor parameter arg0.但如果我添加@ParcelProperty("size")它就可以了.
这是为什么?
更新:
还有其他方法可以使用此库.
我可以删除@ParcelConstructor注释,但后来我会得到错误
Error:Parceler: No @ParcelConstructor annotated constructor and no default empty bean constructor found.
我认为(没有测试过)我也可以使所有构造函数参数可选并添加@JvmOverloads但是有副作用,我必须检查类的所有属性,如果它们是是否为空.
更新2:
这对我有用:
@Parcel
data class Valve(val size: Int? = null)
Run Code Online (Sandbox Code Playgroud)
简而言之,生成的Java类必须具有默认的空构造函数.实现这一目标的一种方法是如上所述 - 所有变量都应该具有默认值.
我有一个API接口,我正在测试View涉及网络调用.
@Config(emulateSdk = 18)
public class SampleViewTest extends RobolectricTestBase {
ServiceApi apiMock;
@Inject
SampleView fixture;
@Override
public void setUp() {
super.setUp(); //injection is performed in super
apiMock = mock(ServiceApi.class);
fixture = new SampleView(activity);
fixture.setApi(apiMock);
}
@Test
public void testSampleViewCallback() {
when(apiMock.requestA()).thenReturn(Observable.from(new ResponseA());
when(apiMock.requestB()).thenReturn(Observable.from(new ResponseB());
AtomicReference<Object> testResult = new AtomicReference<>();
fixture.updateView(new Callback() {
@Override
public void onSuccess(Object result) {
testResult.set(result);
}
@Override
public void onError(Throwable error) {
throw new RuntimeException(error);
}
});
verify(apiMock, times(1)).requestA();
verify(apiMock, times(1)).requestB();
assertNotNull(testResult.get());
}
} …Run Code Online (Sandbox Code Playgroud) 我有一个简单TextView的本地电话号码852112222或(8 5)211 2222.
我需要它可以点击,所以我很自然地使用它android:autoLink="all".
但由于某种原因,我不明白同一电话号码在所有设备上都没有"链接".
在普通的Genymotion设备上它没有用.在我个人的OnePlus2设备上它工作.在不同的设备上测试一堆 - 没有运气.
可能是什么问题?
用户帐户首选项?Android版本?ORM?别的什么?
有没有办法旋转字节数组而不将其解码为Bitmap?
目前在jpeg PictureCallback中我只是将字节数组直接写入文件.但图片是旋转的.我想旋转它们而不解码到位图,希望这将节省我的记忆.
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(data, 0, data.length, o);
int orientation;
if (o.outHeight < o.outWidth) {
orientation = 90;
} else {
orientation = 0;
}
File photo = new File(tmp, "demo.jpeg");
FileOutputStream fos;
BufferedOutputStream bos = null;
try {
fos = new FileOutputStream(photo);
bos = new BufferedOutputStream(fos);
bos.write(data);
bos.flush();
} catch (IOException e) {
Log.e(TAG, "Failed to save photo", e);
} finally {
IOUtils.closeQuietly(bos);
}
Run Code Online (Sandbox Code Playgroud) android ×9
retrofit ×2
android-tabs ×1
kotlin ×1
linkify ×1
mockito ×1
parcelable ×1
parceler ×1
robolectric ×1
rx-java ×1
unit-testing ×1